Undocumented behaviour in jQuery?

jquery

Solution

Yes, this is the expected behavior. Since you have an element on your page with the id "Hello", a property is automiatcally defined on window that points to that element, equivalent to:

window.Hello = document.getElementById("Hello");

this has been around for quite a while in all browsers, i'm not exactly sure where or even if it is documented. jQuery is not doing it.

http://jsfiddle.net/w4YJw/

It is typically suggested to not rely on this since it can be easily broken by defining a variable named the same as the id. It also breaks some functionality, for example, giving a submit button `id="submit"` will make it impossible to call `form.submit()`

Problem

Is the following behaviour documented (i.e. should I rely on it)? Due to an error, I had a dialog box and referred to its id using a bare word in javascript. Surprisingly, I did not get an error and the code worked as intended, The example (on JSFiddle.net here) has a 2 simple dialog boxes ``` <div id="Hello"> <input id="box1" name="box1" type="text" value="not change"> </div> <div id="Goodbye"> <input id="box1" name="box1" type="text" value="not change"> </div> ``` And javascript which is missing the line `Hello = $("#Hello");`. ``` $("#Hello").dialog({ autoOpen:false, modal:true, width:"auto"} ); $("#Hello").dialog("open"); $("#Goodbye").dialog("open"); $('[name="box1"]',Hello).val("test"); ``` Surprisingly, the last line updates the textbox in the `Hello` div, but I can't find anywhere where this behaviour is documented. Is it just an obscure side effect, not to be relied on, or is this a valid jQuery selector style? FYI, I'm using Chrome Version 32.0.1700.107 m.

Original source