Possible Duplicate:
IE/Chrome: are DOM tree elements global variables here?
Description:
Hi there I just noticed that when I use an id on a form it’s available as a global variable
after this. I tested this on Firefox 17.0 and Google Chrome 23.0 both on a Mac. I was kinda surprised when I saw this!
HTML-Code:
<form class="input-form" id="test">
<label for="key">Key</label>
<input type="text" name="key">
</form>
Question:
Does this mean it’s actually a very bad idea to use id’s on a from or am I completely wrong?
Thanks in advance.
If you mean that it’s available in browsers console than it’s ok. It’s just a way browser trying to ease the pain of writing
document.getElementById('test'). At least it’s the case with Chrome. If you open the Chrome console (for example on this page) and just type “sidebar” and hit enter, you’ll see that it returns div with id “sidebar”.To answer directly to your question: it’s completely normal to use an id on a form (or any other node) for that matter.