javascript - Accessing elements with IDs via their global variables -


it not-so-widely-known fact most* web browsers create global variable every element on page id attribute:

html:

<header id="page-header"></header> 


js:

window['page-header'].style.fontfamily = "comic sans ms"; 


questions:

  • is reliable way select elements?
  • any reason use document.getelementbyid instead? guess accessing id'd elements global variables faster document.getelementbyid.


here demo.
*i've tested in latest versions of chrome, firefox, , ie.

i tested on jsperf , chromium v25 getelementbyid faster


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -