Snippet. JavaScript. Get Elements By Class NameA 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; } Updated on: 23 Nov 2024 |
|