Full version: jsB@nk » Snippet » getElementsByClassName: Simple Shorthand Way to Get HTML Element by className with RegExp
URL: https://www.javascriptbank.com/getelementsbyclassname-simple-shorthand-way-to-get-html-element-by-classname-with-regexp.html
A simple JavaScript function to give web developers, web coders, web programmers ability for get HTML elements by their className properties.
Full version: jsB@nk » Snippet » getElementsByClassName: Simple Shorthand Way to Get HTML Element by className with RegExp
URL: https://www.javascriptbank.com/getelementsbyclassname-simple-shorthand-way-to-get-html-element-by-classname-with-regexp.html
<script type="text/javascript">// Created by: Robert Nyman | http://www.robertnyman.com// This script downloaded from www.JavaScriptBank.comfunction getElementsByClassName(className, tag, elm){var testClass = new RegExp("(^|\\\\s)" + className + "(\\\\s|$)");var tag = tag || "*";var elm = elm || document;var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);var returnElements = [];var current;var length = elements.length;for(var i=0; i<length; i++){current = elements[i];if(testClass.test(current.className)){returnElements.push(current);}}return returnElements;}</script>
<p>Some ways to call it:</p><p>To get all a elements in the document with a "info-links" class:<br><code>getElementsByClassName(document, "a", "info-links");</code></p><p>To get all div elements within the element named "container", with a "col" class:<br><code>getElementsByClassName(document.getElementById("container"), "div", "col");</code></p><p>To get all elements within in the document with a "click-me" class:<br><code>getElementsByClassName(document, "*", "click-me");</code></p>