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
- Created by: Robert Nyman
- Web Site: http://www.robertnyman.com
- Posted: May 23, 2008
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.
User Comments
Do you write JavaScripts?
If you do, why not submit them to us?
We'll give you credit and a link back to your Web site.
