+ 1

which method is used to read value of an attribute?

31st Jul 2016, 1:19 AM
Jeevan Pokhrel
Jeevan Pokhrel - avatar
2 Answers
+ 2
The method is: getAttribute (attributeName) Example: var x = document.getElementsByTagName("H1")[0].getAttribute("class"); gets the class of the first H1 element.
31st Jul 2016, 2:16 AM
Nathan “Grimston” Pipes
Nathan “Grimston” Pipes - avatar
+ 1
Depends on what attribute the element has. If it's an id attribute and you want to access its value use getElementById('id'). If it's an element's class attribute value, use getElementsByClassName('class'). This will select one or more elements. The other methods, of which one was mentioned by Nathan here, are querySelector('css selector') and querySelectorAll('css selector').
31st Jul 2016, 5:23 PM
ZinC
ZinC - avatar