
April 3rd, 2005, 09:14 AM
|
|
|
|
Join Date: Apr 2007
Location: Oak Creek Wi, USA
Posts: 5
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
problem with JS functions
Hi, I'm having trouble getting these two functions to work:
Code:
function getLikeElements(tagName, attrName, attrValue){
var startSet;
var endSet = new Array( );
if (tagName){
startSet = document.getElementsByTagName(tagName);
} else {
startSet = (document.all) ? document.all :
document.getElementsByTagName("*");
}
if (attrName){
for (var i = 0; i < startSet.length; i++) {
if (startSet[i].getAttribute(attrName)) {
if (attrValue) {
if (startSet[i].getAttribute(attrName) == attrValue) {
endSet[endSet.length] = startSet[i];
}
} else {
endSet[endSet.length] = startSet[i];
}
}
}
} else {
endSet = startSet;
}
return endSet;
}
function getElementPosition(elemID)
{
var offsetTrail = document.getElementById(elemID);
var offsetLeft = 0;
var offsetTop = 0;
while (offsetTrail)
{
offsetLeft += offsetTrail.offsetLeft;
offsetTop += offsetTrail.offsetTop;
offsetTrail = offsetTrail.offsetParent;
}
if (navigator.userAgent.indexOf("Mac") != -1 && typeof document.body.leftMargin != "undefined")
{
offsetLeft += document.body.leftMargin;
offsetTop += document.body.topMargin;
}
return {left:offsetLeft, top:offsetTop};
}
Two examples return as [object HTMLImageElement] and [object Object] respectively...
disclaimer: I typed these in from an O'Rielly Book, but I *would* like to get them to work, as they will work well is getting posisioning elements for a small web RPG I'm working on.
Thanks!
Mike
|