Posted By

tylerhall on 12/28/06


Tagged

javascript function attribute


Versions (?)

Who likes this?

6 people have marked this snippet as a favorite

noname
happolati
vali29
coprolit
ChristianOttinger
vbmike


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.