getElementsByClassName Deluxe Edition | JavaScriptSource

getElementsByClassName Deluxe Edition

Stuart Colville Aug 8, 2007

General

JavaScript does not provide a function for obtaining elements by class, only by id, name, and tag name. This snippet will accomplish that, and includes optional arguments with defaults and support for multiple classnames in any order. It works in IE, Firefox, Opera, Safari and IE Mac.


Notes

The function has three parameters:

strClass
string containing the class(es) that you are looking for
strTag (optional, defaults to ?*?)
An optional tag name to narrow the search to specific tags e.g. ?a? for links.
objContElm (optional, defaults to document)
An optional object container to search inside. Again this narrows the scope of the search

Examples can be seen here.

Complete instructions can be found on the Muffin Research Labs Web site.

Source Code

Paste this source code into the designated areas.

External file

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


Head

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


Leave a Response

(0 comments)