Built From Developers For Developers
» Kuikie.com » Snippets » JavaScript » Other » JavaScript. Get Elements By Class Name
  Categories
  JavaScript
    Ajax
    IFrame
    Integers
    Other
    Strings
 

JavaScript. Get Elements By Class Name

A great JavaScript function which finds and returns all the DOM elements on the webpage with the same class name.

function getElementsByClassName(classname) {
    var elements = [];
    var re = new RegExp('\\b' + classname + '\\b');
    var all = document.getElementsByTagName("*");
    for (var i = 0; i < all.length; i++) {
        if(re.test(all[i].className)) {
            elements[elements.length] = all[i];
        }
    }
    return elements;
}
 
©2012 Sinevia Ltd Terms of Use         Privacy Policy Valid XHTML 1.0 Transitional