I mean a wrap function like this:
function $(id) { return document.getElementById(id); }
but in some code like this:
oDiv1 = $("postInfoDiv");
oDiv2 = document.getElementById("postInfoDiv");
alert(oDiv1 == oDiv2); // return false
alert(oDiv1.style); // error
alert(oDiv2.style); // correct
alert(document.getElementById("postInfoDiv").style); // correct
I got strange results as the comments imply.
I thought the first alert should return the true since they are the same dom object.
I thought the second alert should alert something like “object” or “CSS StyleDeclaration” but not “defined”.
So what are the problems? Have you ever met this kind of problems?
thanks.
Your
$function is probably being overridden, potentially by a framework.You should try doing
alert( oDiv1.nodeType )to see if it’s a DOM element.alert( oDiv1.length )to see if it’s an empty array because you may be using jQuery on the same page which overrides your$function.oDiv1may be an array-like object containing that item if jQuery is included.oDiv2is an actual DOM reference. You probably need to compareoDiv1[0]tooDiv1, in which you reference the first element in the array which points to the actual dom element to make a fair comparison.The custom
$function will work perfectly but if you’re using a framework function it will return that array-like object instead of the DOM element.You can also rename your function to something like
function getIDwhich would be unique and not conflict with framework$s.