Any difference between a $ wrap function and getElementById?

Your $ function is probably being overridden, potentially by a framework.

Up vote 0 down vote favorite share g+ share fb share tw.

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.

Javascript css link|improve this question edited Aug 18 '10 at 0:54 asked Aug 17 '10 at 15:16Jichao1,74511450 89% accept rate.

2 Have you tried alert(oDiv1. Style) instead of alert(oDiv. Style)?

– KennyTM Aug 17 '10 at 15:17 The second one is maybe just because you never declare an "oDiv", you declare an "oDiv1" and "oDiv2" but never an "oDiv". – Austin Fitzpatrick Aug 17 '10 at 15:18 alert(oDiv1 == oDiv2); is returning true for me as it should. – RoToRa Aug 17 '10 at 15:26 @KennyTM I'm sorry.

I wrote it wrong, it should be alert(oDiv1. Style). – Jichao Aug 17 '10 at 15:26 What browser are you having issues with this?

Seems to be working fine for me when I run this in IE8. Do you have another framework on the page? – epascarello Aug 17 '10 at 15:28.

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. ODiv1 may be an array-like object containing that item if jQuery is included.

ODiv2 is an actual DOM reference. You probably need to compare oDiv10 to oDiv1, in which you reference the first element in the array which points to the actual dom element to make a fair comparison. Function $(id){return document.

GetElementById(id)} $('content') == document. GetElementById('content') true 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 getID which would be unique and not conflict with framework $s.

No array there. – epascarello Aug 17 '10 at 15:25 Why would oDiv1 be an array? – RoToRa Aug 17 '10 at 15:25 He's most likely accidentally using another $ function instead of the one he defined.

Maybe he has Prototype/jQuery on the same page. Otherwise it wouldn't equate to false. – meder Aug 17 '10 at 15:27 @meder: Yes, I have jQuery on the same page.

How did you know it? But I have searched the jQuery source code but could not find a function name $(),though I am sure jQuery has one. – Jichao Aug 17 '10 at 15:36 1 jQuery IS defined as window.

$. You can use jQuery. NoConflict to make jQuery not use $ but it would break your code.

If you try this in a page without jQuery, it'll work perfectly. Soo, I suggest renaming your function or just not using your custom $ because you're already using jQuery. There is no difference between your function and just using gEBI.

– meder Aug 17 '10 at 15:38.

My main concern with this is that it will confuse the heck out of someone the first time they read your code, especially if they are used to coding with a JavaScript framework such as jQuery. For this reason alone I recommend you do not use this particular syntax for your wrap function.

Asking the OP not to use a convenient shorthand because it's common in other libraries doesn't seem like a reasonable answer to his question. Using that logic jQuery shouldn't use $ because Prototype does, or vice versa. You can't assume a JS Project uses a specific library because of a $() function.

– g.d.d. C Aug 17 '10 at 15:33 The accepted answer essentially says the same thing I am - to rename his function because it is conflicting with jQuery. – Justin Ethier Aug 17 '10 at 16:01 I was going to say the exact same thing.

– Patricia Aug 17 '10 at 16:22.

BTW note that even when jQuery is not loaded, Firebug provides its own $ function, which may participate to confusion.

Oh, I debuged this page with Firebug too. – Jichao Aug 18 '10 at 0:53.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions