get elements by class


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. function getElementsByClass( searchClass, domNode, tagName) {
  2. if (domNode == null) domNode = document;
  3. if (tagName == null) tagName = '*';
  4. var el = new Array();
  5. var tags = domNode.getElementsByTagName(tagName);
  6. var tcl = " "+searchClass+" ";
  7. for(i=0,j=0; i<tags.length; i++) {
  8. var test = " " + tags[i].className + " ";
  9. if (test.indexOf(tcl) != -1)
  10. el[j++] = tags[i];
  11. }
  12. return el;

URL: http://www.anyexample.com/webdev/javascript/javascript_getelementsbyclass_function.xml

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.