Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
170
rated 0 times [  173] [ 3]  / answers: 1 / hits: 16523  / 9 Years ago, mon, april 13, 2015, 12:00:00

Here is a demo code:



<div id=demo myAttribute=ok></div>


Here are 2 things I want

1) the attribute name, ie: 'myAttribute'.

2) the element that contains the attribute 'myAttribute', ie: the div.


More From » html

 Answers
29

To get a NodeList of Nodes that match a selector



var list = document.querySelectorAll('[myAttribute]');


list will be Array-like but not inherit from Array. You can loop over it with for and list.length






To get a NamedNodeMap of the attributes on an Element



var nnm = elem.attributes;


nnm will be Array-like but not inherit from Array. You can loop over it with for and nnm.length






To get the value of an attribute on an Element use .getAttribute



var val = elem.getAttribute('myAttribute');


val will be null if there is no such attribute






To test the existance of an attribute on an Element use .hasAttribute



var b = elem.hasAttribute('myAttribute');


b will be a Boolean value, true or false


[#67098] Friday, April 10, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
lucillemariselal

Total Points: 108
Total Questions: 97
Total Answers: 119

Location: Thailand
Member since Thu, May 6, 2021
3 Years ago
;