Skip to content Skip to sidebar Skip to footer

Getelementsbyclassname Ie Resolution Issue

I am having issues figuring out how to resolve the getElementsByClassName issue in IE. How would I best implement the robert nyman (can't post the link to it since my rep is only

Solution 1:

I was curious to see what a jQuery version of your function would look like, so I came up with this:

functionshowDesc(name) {
    var e = $("#" + name);
    $(".description.show").removeClass("show");
    if(e.attr("class") == "description") {
        e.addClass("show");
    } elseif(e.hasClass("description") && e.hasClass("show")) {
        return;
    }
}

Solution 2:

This should support multiple classes.

functiongetElementsByClassName(findClass, parent) {

  parent = parent || document;
  var elements = parent.getElementsByTagName('*');
  var matching = [];

  for(var i = 0, elementsLength = elements.length; i < elementsLength; i++){

    if ((' ' + elements[i].className + ' ').indexOf(findClass) > -1) {
      matching.push(elements[i]);
    }

  }

  return matching;

}

You can pass in a parent too, to make its searching the DOM a bit faster.

If you want getElementsByClassName('a c') to match HTML <div class="a b c" /> then try changing it like so...

var elementClasses = elements[i].className.split(/\s+/),
    matchClasses = findClass.split(/\s+/), // Do this out of the loop :)
    found = 0;

for (var j = 0, elementClassesLength = elementClasses.length; j < elementClassesLength; j++) {

    if (matchClasses.indexOf(elementClasses[j]) > -1) {
        found++;
    }

}

if (found == matchClasses.length) {
   // Push onto matching array
}

If you want this function to only be available if it doesn't already exist, wrap its definition with

if (typeofdocument.getElementsByClassName != 'function') { }

Solution 3:

Even easier jQuery solution:

$('.service').click( function() {
    var id = "#" + $(this).attr('id') + 'rt';
    $('.description').not(id).hide();
    $( id ).show();
}

Why bother with a show class if you are using jQuery?

Solution 4:

Heres one I put together, reliable and possibly the fastest. Should work in any situation.

function$class(className) {
    var children = document.getElementsByTagName('*') || document.all;
    var i = children.length, e = [];
    while (i--) {
        var classNames = children[i].className.split(' ');
        var j = classNames.length;
        while (j--) {
            if (classNames[j] == className) {
                e.push(children[i]);
                break;
            }
        }
    }
    return e;
}

Solution 5:

I used to implement HTMLElement.getElementByClassName(), but at least Firefox and Chrome, only find the half of the elements when those elements are a lot, instead I use something like (actually it is a larger function):

getElmByClass(clm, parent){
   // clm:  Array of classesif(typeof clm == "string"){ clm = [clm] }
   var i, m = [], bcl, re, rm;
   if (document.evaluate) {    // Non MSIE browsers
      v = "";
      for(i=0; i < clm.length; i++){
         v += "[contains(concat(' ', @"+clc+", ' '), ' " + base[i] + " ')]";
      }
      c = document.evaluate("./"+"/"+"*" + v, parent, null, 5, null);
      while ((node = c.iterateNext())) {
          m.push(node);
      }
   }else{                  // MSIE which doesn't understand XPATH
      v = elm.getElementsByTagName('*');
      bcl = "";
      for(i=0; i < clm.length; i++){
          bcl += (i)? "|":"";
          bcl += "\\b"+clm[i]+"\\b";
      }
      re = newRegExp(bcl, "gi");
      for(i = 0; i < v.length; i++){
         if(v.className){
             rm = v[i].className.match(bcl);
             if(rm && rm.length){      // sometimes .match returns an empty array so you cannot use just 'if(rm)'
                 m.push(v[i])
             }
         }
      }
    }
    return m;
}

I think there would be a faster way to iterate without XPATH, because RegExp are slow (perhaps a function with .indexOf, it shuld be tested), but it is working well

Post a Comment for "Getelementsbyclassname Ie Resolution Issue"