getElementsByClassName - The Ultimate Version | JavaScriptSource

getElementsByClassName - The Ultimate Version

Robert Nyman May 23, 2008

General

This JavaScript function will obtain elements by class name. If you want to apply a certain event to some elements, you can loop through the elements in the page and then apply the event accordingly, assigning them to those elements that have a certain class name.


Notes

Some ways to call it:

To get all a elements in the document with a "info-links" class:
getElementsByClassName(document, "a", "info-links");

To get all div elements within the element named "container", with a "col" class:
getElementsByClassName(document.getElementById("container"), "div", "col");

To get all elements within in the document with a "click-me" class:
getElementsByClassName(document, "*", "click-me");

Source Code

Paste this source code into the designated areas.

External file

Paste this code into an external JavaScript file named: classNameUV.js


Head

Paste this code into the HEAD section of your HTML document.


Leave a Response

(0 comments)