0

Where is the documentation that explains why/how this works? jQuery interprets an undeclared variable as an id. I find this odd, and encountered by chance. How does it work?

HTML

<div id="wrapper">

   <input id="a" value="click" type="button" />

</div>

JS

$(wrapper).on("click", "#a", function(){

  alert("test");

});

JSBIN

1252748
  • 14,597
  • 32
  • 109
  • 229
  • 2
    That's a quirk of JavaScript, not jQuery. – zzzzBov Nov 15 '13 at 19:33
  • 3
    Here's a hint: it has nothing to do with jquery. – Kevin B Nov 15 '13 at 19:33
  • 1
    @zzzzBov: That's a quirk of Microsoft Internet Explorer, not JavaScript. :P – Amadan Nov 15 '13 at 19:36
  • Related: http://stackoverflow.com/questions/19776533/can-i-use-the-id-of-an-html-element-as-a-variable-in-javascript – megawac Nov 15 '13 at 19:37
  • @Amadan, all browsers do it, not just IE. It's a quirk of the language for backwards compatibility. – zzzzBov Nov 15 '13 at 19:37
  • It's been standard in all browsers since ff 14.0 – megawac Nov 15 '13 at 19:37
  • All browsers started doing it to make them compatible with IE's silliness. JavaScript outside browsers (like in Node.js), obviously, does not do this, so it cannot be a quirk of JavaScript. – Amadan Nov 15 '13 at 19:38
  • @Amadan, i suppose to be more technically correct I should have said it's a quirk of a JavaScript module, but the point I was making was just that it was not a feature from jQuery. – zzzzBov Nov 15 '13 at 19:41
  • @zzzzBov: I know. It was half joke, half jab at Microsoft in the Bad Old Days, and half hypercorrection. (It was a rather large comment. :D ) – Amadan Nov 15 '13 at 19:43
  • Maybe, of the DOM api is more appropriate? – Kevin B Nov 15 '13 at 19:46

1 Answers1

4

It doesn't. Javascript/the DOM does.

The id attribute is used to create a global variable that points to each element with an id.

This behaviour has been in Internet Explorer for years, and is now standardised in HTML5.

lonesomeday
  • 233,373
  • 50
  • 316
  • 318