-4

I have a situation where I need to access the header checkbox of the table I'm doing it with

document.getElementsByClassName("table_th_label_div")[0];

But as some of the older browser does not support .getElementsByClassName I need an alternative appproach for the same

Kindly Help Thanks in advance

zzlalani
  • 22,960
  • 16
  • 44
  • 73
Naim FS
  • 61
  • 9
  • IE8 supports `querySelectorAll` (sort of) – elclanrs Feb 10 '14 at 08:17
  • Can you please tell me reason for down voting it.? – Naim FS Feb 10 '14 at 08:18
  • 1
    I suppose because your question doesn't show research on your part. There are a few dups http://stackoverflow.com/questions/9568969/getelementsbyclassname-ie8-object-doesnt-support-this-property-or-method, http://stackoverflow.com/questions/13261506/getelementsbyclassname-in-ie8, http://stackoverflow.com/questions/10484467/ie-8-object-doesnt-support-property-or-method-getelementsbyclassname – elclanrs Feb 10 '14 at 08:20

1 Answers1

2

This solution may help. This is the getElementsByClassName function implemented in pure javascript, that works in IE.

/*
    Developed by Robert Nyman, http://www.robertnyman.com
    Code/licensing: http://code.google.com/p/getelementsbyclassname/
*/  
var getElementsByClassName = function (className, tag, elm){
    if (document.getElementsByClassName) {
        getElementsByClassName = function (className, tag, elm) {
            elm = elm || document;
            var elements = elm.getElementsByClassName(className),
                nodeName = (tag)? new RegExp("\\b" + tag + "\\b", "i") : null,
                returnElements = [],
                current;
            for(var i=0, il=elements.length; i<il; i+=1){
                current = elements[i];
                if(!nodeName || nodeName.test(current.nodeName)) {
                    returnElements.push(current);
                }
            }
            return returnElements;
        };
    }
    else if (document.evaluate) {
        getElementsByClassName = function (className, tag, elm) {
            tag = tag || "*";
            elm = elm || document;
            var classes = className.split(" "),
                classesToCheck = "",
                xhtmlNamespace = "http://www.w3.org/1999/xhtml",
                namespaceResolver = (document.documentElement.namespaceURI === xhtmlNamespace)? xhtmlNamespace : null,
                returnElements = [],
                elements,
                node;
            for(var j=0, jl=classes.length; j<jl; j+=1){
                classesToCheck += "[contains(concat(' ', @class, ' '), ' " + classes[j] + " ')]";
            }
            try {
                elements = document.evaluate(".//" + tag + classesToCheck, elm, namespaceResolver, 0, null);
            }
            catch (e) {
                elements = document.evaluate(".//" + tag + classesToCheck, elm, null, 0, null);
            }
            while ((node = elements.iterateNext())) {
                returnElements.push(node);
            }
            return returnElements;
        };
    }
    else {
        getElementsByClassName = function (className, tag, elm) {
            tag = tag || "*";
            elm = elm || document;
            var classes = className.split(" "),
                classesToCheck = [],
                elements = (tag === "*" && elm.all)? elm.all : elm.getElementsByTagName(tag),
                current,
                returnElements = [],
                match;
            for(var k=0, kl=classes.length; k<kl; k+=1){
                classesToCheck.push(new RegExp("(^|\\s)" + classes[k] + "(\\s|$)"));
            }
            for(var l=0, ll=elements.length; l<ll; l+=1){
                current = elements[l];
                match = false;
                for(var m=0, ml=classesToCheck.length; m<ml; m+=1){
                    match = classesToCheck[m].test(current.className);
                    if (!match) {
                        break;
                    }
                }
                if (match) {
                    returnElements.push(current);
                }
            }
            return returnElements;
        };
    }
    return getElementsByClassName(className, tag, elm);
};
Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
RealDeepak
  • 823
  • 6
  • 9
  • Yes, I understand. But the solution is too long and can't reflect good in direct answer. So that's why I given him solution in link. And Its work. – RealDeepak Feb 10 '14 at 08:33
  • Thanks Quantas. But again if users go to this link then they will fully understand the concept of how it is working. But anyways thanks. – RealDeepak Feb 10 '14 at 08:38
  • Sure, that's why you have the link in the first place. But if one day the person doesn't renew their domain, then this answer would be useless, which is why it's preferred to have the main point (in this case the code) in the answer. – Qantas 94 Heavy Feb 10 '14 at 08:44