Why shouldn't I access elements more "directly" (elemId.innerHTML)
Since, according to the others in this thread, a link to an id id is not fully supported .
So, what I think you should do is save their selection in var and then reference var.
Try instead
var color = document.getElementById('color'); color.innerHTML = 'something';
The reason this would be good is because doing a search in the DOM is an expensive process that is memory-wise. And therefore, if you store a reference to an element in a variable, it becomes static. This way you are not doing a search every time you want .doSomething()
to it.
Note that javascript libraries tend to add padding functions to increase overall browser support for functions. which would be useful to use, for example, a jquery selector on top of pure javascript. Although, if you are really concerned about memory / performance, native JS usually wins speed tests. (jsperf.com is a good tool for measuring speed and making comparisons.)
source share