getElementsByClass

By koooer

from


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

How it works

It’s simple. It works just how you think getElementsByClass would work, except better.

  • 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“. Or, if you’re like me, and you said Good Bye to IE5, you can use the “*â€? asterisk as a catch-all (meaning ‘any element).
  • 7 Responses to “getElementsByClass”

    1. Eduardo Ramos Says:

      The following function is a straightforward generalization of getElements ByClass() that allows to search for the attribute “class” or other useful atributes like “lang” or some custom made attribute that could be useful to organize your own content.

      /* Created by 2mdc.com
      url: http://www.2mdc.com
      more info: http://2mdc.com/blogs/programacion-web/getelementsbyattribute

      This is an extension of getElementsByClass by Dustin Diaz: http://www.dustindiaz.com/

      */

      function getElementsByAttribute(attribute,name,node,tag) {

      //This is the array that stores the data
      var results = new Array();

      //Set up default values for node and tag

      if ( node == null )
      node = document;
      if ( tag == null )
      tag = ‘*’;

      var elements = node.getElementsByTagName(tag);
      var elements_length = elements.length;

      //The required regular expression
      var pattern = new RegExp(”(\\b)”+name+”(\\b)”);

      //We search for the required elements

      for (i = 0, j = 0; i

    2. Chrome Wheels Resource Says:

      Chrome Wheels and Rims

      Using certian cleaners can cause your rims to…

    3. XRumakTheBest Says:

      XRumer 4.0 is the perfect tool for advertisement!
      It’s have CAPTCHA recognizer, email verificator, and a lot of other functions…

      But. I forgot link to it :(

      Can you give me link to the xrumer description? screenshots, etc.

      Thanks

    4. Timi Says:

      https://support.brooklyn.cuny.edu/member.php?u=106

    5. Creelttheatly Says:

      Do you know if some company use the same marketing concept as Webkinz?

      I’m writing a few articles right now and want to get some info and opinions.

    6. chetoummisito Says:

      intermittently non-regulatory as belag endlessly viagra periodically scintilla while

    7. Gennick Says:

      Hello. And Bye.

    Leave a Reply