We Recommend

Pro JavaScript Techniques Pro JavaScript Techniques
Pro JavaScript Techniques is the ultimate JavaScript book for the modern web developer. It provides everything you need to know about modern JavaScript, and shows what JavaScript can do for your web sites. This book doesn't waste any time looking at things you already know, like basic syntax and structures.


Posted By

tylerhall on 12/28/06


Tagged

javascript function attribute


Versions (?)


Who likes this?

3 people have marked this snippet as a favorite

noname
happolati
vali29


Get Elements By Attribute


Published in: JavaScript 


  1. function getElementsByAttribute(oElm, strTagName, strAttributeName, strAttributeValue){
  2. var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
  3. var arrReturnElements = new Array();
  4. var oAttributeValue = (typeof strAttributeValue != "undefined")? new RegExp("(^|\\s)" + strAttributeValue + "(\\s|$)", "i") : null;
  5. var oCurrent;
  6. var oAttribute;
  7. for(var i=0; i<arrElements.length; i++){
  8. oCurrent = arrElements[i];
  9. oAttribute = oCurrent.getAttribute && oCurrent.getAttribute(strAttributeName);
  10. if(typeof oAttribute == "string" && oAttribute.length > 0){
  11. if(typeof strAttributeValue == "undefined" || (oAttributeValue && oAttributeValue.test(oAttribute))){
  12. arrReturnElements.push(oCurrent);
  13. }
  14. }
  15. }
  16. return arrReturnElements;
  17. }

Report this snippet 

You need to login to post a comment.