»
EnglishFrenchVietnamese

Print - getElementsByClass - JavaScriptBank.com

Full version: jsB@nk » Snippet » getElementsByClass
URL: https://www.javascriptbank.com/getelementsbyclass.html

getElementsByClass © JavaScriptBank.comA quick and elegant way of grabbing elements by a class name. JavaScript provides functions for obtaining elements by id, name, and tag name, but not by class. This function will do the trick!

Full version: jsB@nk » Snippet » getElementsByClass
URL: https://www.javascriptbank.com/getelementsbyclass.html



JavaScript
<script type="text/javascript">// Created by: Dustin Diaz :: http://www.dustindiaz.com/function getElementsByClass(searchClass,node,tag) {  var classElements = new Array();  if (node == null)    node = document;  if (tag == null)    tag = '*';  var els = node.getElementsByTagName(tag);  var elsLen = els.length;  var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");  for (i = 0, j = 0; i < elsLen; i++) {    if (pattern.test(els[i].className) ) {      classElements[j] = els[i];      j++;    }  }  return classElements;}</script><!--    This script downloaded from www.JavaScriptBank.com    Come to view and download over 2000+ free javascript at www.JavaScriptBank.com-->