New Way of Accessing DOM
It was a simple textbox like
On a button click i just wrote like :
<input type="text" id="userName" />
and surprisingly it worked.
kindly help me resolve this, wishing for a innovation soon :)
This is a holdover from older days, and has a gotcha- if you have any variables with the same name as an id, the variable is exposed and the id is not. Also, the behavior is not in any specification, and is not guaranteed to continue in future versions of browsers that use it now. Better to use getElementById...
I tested and seems to works, also with other HTML tags::
- But, for safe, I think it is better to use document.getElementById('id') or with jQuery $('#id') .
alert(userName.innerHTML); // <b>test</b>
You are correct. i tested it at my end and surprisingly it worked for any HTML tag.
So i understand for precautions its better to use getElementById/Jquery DOM accessor. But was just wondering whatr's the reason/cause for this actually conceptually.
Even its not the case with IE only it works with any browser, so we can't take it as a constraint of browser only. Any conceptual reason for this behaviour?
This is also something new for me too.
Maybe someone professional know the answer.
Anyway, in the console of firebug addon in Mozilla appear this warning:
"Element referenced by ID/NAME in the global scope. Use W3C standard document.getElementById() instead".
Thanks for your help, i think what Firefox says is something which seems to be suprising, it means by default for each element a DOM is already available pre made. Its convention to use getEleementById, but its availability is already there though.
So was just wondering is this the feature of browser or so, because each browser provides these, so might be a feature available above the limitations of browser.
So just wondering about some detailed inputs for the reason. Hope so we learn something new :)
What's your question? As mrhoo said, it's still allowed occasionally for backwards compatibility, other than that, it's entirely wrong and you should never use it. Nothing to be learned.
Originally Posted by jatinkindra