0

I ran into a line of jquery that looks like the below. It works fine in Chrome however when I try it in IE, I get a error saying "keyboardLanguageSelect is undefined."

$(keyboardLanguageSelector).children('option').length == 0

If I write it like the below it works in both.

$('#keyboardLanguageSelector').children('option').length == 0

Question: Why does chrome automatically find this where IE will not process it? Does anyone have a link that talks about these differences in the browser?

KungFuMaster
  • 101
  • 7
  • I guess that is what I find interesting, it works fine in Chrome exactly how it is written. – KungFuMaster Nov 29 '16 at 19:51
  • 1
    because magic. :) in some browsers dom id's (and sometimes names) are automagically turned into global vars, which can be problematic sometimes, and useful other times. A common case that this causes a problem is when you give your submit button an `id="submit"`, then try to call `form.submit`. it will fail because `form.submit` is a button, not a function. :) – Kevin B Nov 29 '16 at 19:51
  • '$(keyboardLanguageSelector).children('option').length == 0' does not work on chrome for me, are you sure that is the exact code that is working on Chrome but not IE? (Chrome 56.0.2924.3 dev (64-bit)) – Jacob Linney Nov 29 '16 at 19:52
  • 1
    @KungFuMaster no, it doesn't. `$(keyboardLanguageSelector)` throws an error in console in Chrome 57.0.2931.1 (Official Build) canary: `Uncaught ReferenceError: keyboardLanguageSelector is not defined`. – canon Nov 29 '16 at 19:52
  • haha, that is kind of what it felt, like I realize it is bad but at the same time I am intrigued with why it worked in the first place. – KungFuMaster Nov 29 '16 at 19:52
  • 3
    http://stackoverflow.com/questions/3434278/do-dom-tree-elements-with-ids-become-global-variables – Kevin B Nov 29 '16 at 19:53
  • Ah, Kevin likely has the right of it. DOM id's added as globals inconsistently... – canon Nov 29 '16 at 19:53
  • Yeah that looks like that what it is – Jacob Linney Nov 29 '16 at 19:55
  • @Kevin B, Thanks for the link! – KungFuMaster Nov 29 '16 at 19:58

0 Answers0