What is the difference between document.getElementById('mybox') and mybox? [duplicate]
Possible Duplicate:
document.getElementById(“someId”) Vs. someId
For example I have an element with id="mybox"
attribute, is there any difference between calling it with document.getElementById('mybox')
and mybox
directly, as I see both work same in most browsers? The jsfiddle shows live example http://jsfiddle.net/usmanhalalit/TmS3k/
If there is no difference then why document.getElementById('mybox')
is so popular,开发者_开发问答 is it a bad practice to call mybox
directly?
Some browsers in some rendering modes will create a global variable for each element with an id.
It is non-standard, won't work everywhere and definitely can't be depended upon.
They don't "both work the same". IE introduced making element ids into global variables, other browsers copied it to some extent but don't fully support it. It's considered a very bad idea, just don't do it.
mybox.value
does not work in most cases. I believe IE is the only browser (and only some versions of it) that would support it. In my Firefox browser, I get mybox not defined
error message in the console.
精彩评论