getElementsByClassName Deluxe Edition

By Stuart Colville on Aug 8, 2007


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.


  • Created by: Stuart Colville
  • Web Site:
  • Posted: August 8, 2007

The function has three parameters:

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


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