I need to take some DIVs that have a particular class name and throw them into some conditions. Is there a getElementByClass() function native to JavaScript? I’ve seen numerous internet posts about this but nothing that mentions an “official” JavaScript function and I can’t see the rationale behind that… Has this been left out of JavaScript entirely? If so, why? It seems as if it would be a pretty necessary function to have.
IMHO, there is no native javascript function to do this.
I don’t really know why not
I came to the same conclusion but I eventually found this script. Seems overkill, though…
The native dom element method is getElementsByClassName. It’s a more recent addition, so adding backwards compatibility to your code is a really good idea.
Just a thought, why don’t you use a javascipt framework like jQuery?
Instead of document.getElementById(“someId”) you can just do $(“#someId”)
And instead of document.getElementByClassNames(“someclass”) you can do $(“.someclass”)
And even more extended, if you want an element within an element with id “someId” that has css class “tf” and is a checkbox you can do
$(“#someId input[type=‘checkbox’].tf”)
Basically it completely supports CSS3 and is also completely cross-browser compatible.