Javascript to get elements by its attribute
<body> <span someAttribute="xyz">.....</span> ... <div> ... <span someAttribute="abc">.....</span> ... <div someAttribute="pqr">.....</div> ... </div> </body> Here is an example html page. I need to select html elements according to its attributes, I can get the values ββof the getAttribute() attribute, but first I need to select all the elements.
How in javascript to get elements that have an attribute name like "someAttribute". Once I get the elements, I can get the attribute values ββand use my function.
Note. I want to do this without using jquery.
store each element in an array, loop through each element, and if the element contains the attribute someAttribute do somgthing.
var arr_elms = []; arr_elms = document.body.getElementsByTagName("*"); var elms_len = arr_elms.length; for (var i = 0; i < elms_len; i++) { if(arr_elms[i].getAttribute("someAttribute") != null){ alert("FOUND : " + arr_elms[i].getAttribute("someAttribute")); } } I found a fragment called getElementsByAttribute(doc, tagArray, attribute, attributeValue)
You can try working on the fiddle: http://jsfiddle.net/Yx7EU/
Hope this helps.