04-28-2013, 10:28 PM | #1 |
Registered User
Join Date: Jul 2009
Posts: 191
|
getElementsByAttribute
Ever run into a situation where you want to get an array of all elements with a specific attribute? Or even want elements with a certain value for that chosen attribute as well? Just add this <a href=... detail at JavaScriptBank.com - 2.000+ free JavaScript codes
How to setup Step 1: Copy & Paste JavaScript code below in your HEAD section JavaScript Code:
<script type="text/javascript"> /* Copyright Robert Nyman, http://www.robertnyman.com Free to use if this text is included */ function getElementsByAttribute(oElm, strTagName, strAttributeName, strAttributeValue){ var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName); var arrReturnElements = new Array(); var oAttributeValue = (typeof strAttributeValue != "undefined")? new RegExp("(^|\\s)" + strAttributeValue + "(\\s|$)") : null; var oCurrent; var oAttribute; for(var i=0; i<arrElements.length; i++){ oCurrent = arrElements[i]; oAttribute = oCurrent.getAttribute(strAttributeName); if(typeof oAttribute == "string" && oAttribute.length > 0){ if(typeof strAttributeValue == "undefined" || (oAttributeValue && oAttributeValue.test(oAttribute))){ arrReturnElements.push(oCurrent); } } } return arrReturnElements; } </script> <!-- This script downloaded from www.JavaScriptBank.com Come to view and download over 2000+ free javascript at www.JavaScriptBank.com --> |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
Thread Tools | |
Display Modes | Rate This Thread |
|
|