Do DOM tree elements with IDs become global properties?
What is supposed to happen is that ‘named elements’ are added as apparent properties of the document
object. This is a really bad idea, as it allows element names to clash with real properties of document
.
IE made the situation worse by also adding named elements as properties of the window
object. This is doubly bad in that now you have to avoid naming your elements after any member of either the document
or the window
object you (or any other library code in your project) might want to use.
It also means that these elements are visible as global-like variables. Luckily in this case any real global var
or function
declarations in your code shadow them, so you don't need to worry so much about naming here, but if you try to do an assignment to a global variable with a clashing name and you forget to declare it var
, you'll get an error in IE as it tries to assign the value to the element itself.
It's generally considered bad practice to omit var
, as well as to rely on named elements being visible on window
or as globals. Stick to document.getElementById
, which is more widely-supported and less ambiguous. You can write a trivial wrapper function with a shorter name if you don't like the typing. Either way, there's no point in using an id-to-element lookup cache, because browsers typically optimise the getElementById
call to use a quick lookup anyway; all you get is problems when elements change id
or are added/removed from the document.
Opera copied IE, then WebKit joined in, and now both the previously-unstandardised practice of putting named elements on document
properties, and the previously-IE-only practice of putting them on window
are being standardised by HTML5, whose approach is to document and standardise every terrible practice inflicted on us by browser authors, making them part of the web forever. So Firefox 4 will also support this.
What are ‘named elements’? Anything with an id
, and anything with a name
being used for ‘identifying’ purposes: that is, forms, images, anchors and a few others, but not other unrelated instances of a name
attribute, like control-names in form input fields, parameter names in <param>
or metadata type in <meta>
. ‘Identifying’ name
s are the ones that should be avoided in favour of id
.
In what standard is specified that elements with an id also get a global variable?
Its to do with the Document Object Model (DOM), which defines the logical structure of documents and the way a document is accessed and manipulated. it is not best practice and its better to use document.getElementById(); to avoid naming collisions etc.
Here is an example answer in response to this question previously.
id attribute on form element results in global variable
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.
Related Topics
Double Quotes Within PHP Script Echo
Pass JavaScript Variable to PHP Via Ajax
JavaScript Equivalent to PHP Explode()
How to Modify Style to HTML Elements (Styled Externally With Css) Using Js
How to Prevent Form from Being Submitted
Difference Between Properties and Attributes in Html
Set Cookie and Get Cookie With JavaScript
How to Make HTML Element Resizable Using Pure JavaScript
Refresh a Page Using JavaScript or Html
Does Html5/Canvas Support Double Buffering
How to Escape a Single Quote ( ' ) in JavaScript
Getting the Parent Div of Element
How to Clear an HTML File Input With JavaScript
Using Queryselector With Ids That Are Numbers
Html5 Canvas Todataurl Returns Blank
Js Function Named 'Animate' Doesn't Work in Chrome, But Works in Ie