web developer toolbox

Home » JavaScript » DOM
Published: 2011 Mar. 29 Updated: 2011 Mar. 29

Company Name

getElementsByClassName for IE less than 9

...getElementsByClassName work-around

Comments:

Default example provided by web developer toolbox.
getElementsByClassName work-around for IE < 9
syntax: getElementsByClassName(classname, node, tag);
parameters:
  • multiple class names are each separated by a space
  • node is an html element, obtained by getElementById for example
  • only one tag name allowed
  • classname is required, node and tag are optional
example: elems = getElementsByClassName("class1 class2", document.forms[0], "p");
returns: above example returns a collection of all P elements inside the first form matching ALL criteria, including elements with class="class1 class2 class3", but not elements with just class="class1"
drawbacks: slightly different way to invoke: straight function call with 3 parameters (last 2 optional), instead of DOM method with 1 parameter - so that could contribute to a little bit of confusion
note: this does not redefine DOM method, which could still be invoked

Results:

Welcome to web developer toolbox!

Smart

Very smart!

Generated html:

Original html:

JavaScript:

Embedded css: