getElementsByAttribute
document.getElementsByAttribute=function(attrN,attrV,multi){ attrV=attrV.replace(/\|/g,'\\|').replace(/\[/g,'\\[').replace(/\(/g,'\\(').replace(/\+/g,'\\+').replace(/\./g,'\\.').replace(/\*/g,'\\*').replace(/\?/g,'\\?').replace(/\//g,'\\/'); var multi=typeof multi!='undefined'? multi: false, cIterate=document.getElementsByTagName('*'), aResponse=[], attr, re=new RegExp(multi?'\\b'+attrV+'\\b':'^'+attrV+'$'), i=0, elm; while((elm=cIterate.item(i++))){ attr=elm.getAttributeNode(attrN); if(attr && attr.specified && re.test(attr.value) ) aResponse.push(elm); } return aResponse; }
Usage
Syntax to consume this function :
document.getElementsByAttribute([string attributeName],[string attributeValue],[boolean isCommaHyphenOrSpaceSeparatedList:false])
Example :
document.getElementsByAttribute("type", "button", false);
Where
1. "type" is attributename of any element.
2. "button" is value of attribute
3. "false" is whether - isCommaHyphenOrSpaceSeparatedList or not.
0 Responses to "Custom Javascript function - getElementsByAttribute"
Post a Comment