Results 1 to 4 of 4

Thread: getElementsByClassName workaround?

  1. #1
    Join Date
    Jan 2007

    getElementsByClassName workaround?

    I used getElementsByClassName function and it works greate on new version of Firefox. However, it doesn't do work on IE and old version of Firefox (2.0)
    Is there another approach to this?
    function removeRow(element)
    var className = element.className;
    var parentNode = element.parentNode;
    var deletedElement;
    var deleteElements = document.getElementsByClassName(className);
    for(var i=deleteElements.length - 1; i >= 0; i--)
    deletedElement = deleteElements[i];

    if(deletedElement.tagName === 'BR')
    break; // exit the loop after we delete the BR because if we keep going we'll delete other rows.

  2. #2
    Join Date
    Sep 2009
    PHP Code:
    <div class="test">Div 1</div>
    div class="test">Div 2</div>

    script language="javascript">
    document.getElementsByClassName = function(cls) {
    found = [];
    rx = new RegExp('(^| )' cls '( |$)');
    els this.getElementsByTagName('*');
        for (var 
    i=0i<els.lengthi++) {
    elClass els[i].className;
            if (
    rx.test(elClass)) {

    divs document.getElementsByClassName('test');

  3. #3
    Join Date
    Feb 2006
    You can also return an array of the elements that include any number of space separated class names.

    function getHTMLClasses(css, root){
    	root= root || document;
    	css= css.split(/ +/);
    	var who, tem, A= [], L2= css.length,
    	collection= root.getElementsByTagName('*'),
    	L= collection.length;
    		who= collection[--L];
    		for(var i= 0, z= css.length; i<z; i++){
    			if(who.className.indexOf(css[i])== -1){
    				who= null;
    				i= z;
    		if(who) A[A.length]= who;
    	return A;
    // eg: getHTMLClasses('hotCss offCss', document.body)
    Last edited by mrhoo; 10-14-2009 at 10:27 PM.

  4. #4
    Join Date
    Mar 2007
    Here is another method

    if( !typeof document.getElementsByClassName == 'function'){
        Object.prototype.getElementsByClassName = function(cn) {
            if (!this) return null;
            for (var r=[], e=this.getElementsByTagName('*'), i=e.length; i--;)
            if ( e[i].className.indexOf(cn)>-1) r.push(e[i]); 
            return r;  
    Whatever you do, don't post something like this on Stack Overflow, you get berated for posting something that returns an array.

    WTF are you supposed to return then... Cream Cakes?

    A Node list, if I am not mistaken is a collection of objects and an object is nothing more than an array or typeof array, ergo, no matter which method you try, its going to be an array.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center

Recent Articles