please dont rip this site

JScript / JavaScript Extension Feature


A quick and elegant way of grabbing elements by a className by Dustin Diaz

Supply a class name as a string.

(optional) Supply a node. This can be obtained by getElementById, or simply by just throwing in “document” (it will be document if don’t supply a node)). It’s mainly useful if you know your parent and you don’t want to loop through the entire D.O.M.

(optional) Limit your results by adding a tagName. Very useful when you’re toggling checkboxes and etcetera. You could just supply “input“.

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
	return classElements;

Here is a sample useage:

<script type="text/javascript">
function showFoo() {
	var el = getElementsByClass(document,'foo','*');
	<div id="wrapper">
		<p>Searching <span class="foo">foo</span></p>
		<p class="foo">foo</p>
		<p class="bar Foo">boo Far</p>
		<p class="bar foo">boo far</p>
		<p class="foo bar">foo bar</p>
		<p class="fooo">fooo</p>
		<p class="foobar">foobar</p>
		<p class="bar foo bars">bar foo bars</p>
		<p class="boofar">boofar</p>
		<button onclick="showFoo();">Show Total foo</button>

Pushing the button shows "5"

See also:

file: /Techref/inet/iis/jscript/htm/getElementsByClass.htm, 2KB, , updated: 2005/12/9 16:41, local time: 2024/6/16 16:53,

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF=""> JScript / JavaScript Extension Feature</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.

Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?

  PICList 2024 contributors:
o List host: MIT, Site host, Top posters @none found
- Page Editors: James Newton, David Cary, and YOU!
* Roman Black of Black Robotics donates from sales of Linistep stepper controller kits.
* Ashley Roll of Digital Nemesis donates from sales of RCL-1 RS232 to TTL converters.
* Monthly Subscribers: Gregg Rew. on-going support is MOST appreciated!
* Contributors: Richard Seriani, Sr.

Welcome to!