I’m trying to extract value
property from a HTML file. I used querySelectorAll
to get all the nodes in the file. Could anyone please help how do I only fetch value
property from the file.
JavaScript
x
2
1
const nodes = document.querySelectorAll("add")
2
console.log(nodes)
JavaScript
1
5
1
<div>
2
<add value="abc"></add>
3
<add value="def"></add>
4
<add value="ghi"></add>
5
</div>
Advertisement
Answer
Be sure to check that the selected nodes have the attribute value
by adding [value]
to the query.
Note: here i use the ES6 spread operator to get the NodeList as an array.
JavaScript
1
2
1
const nodes = document.querySelectorAll("add[value]")
2
console.log([nodes].map(n => n.getAttribute("value")))
JavaScript
1
5
1
<div>
2
<add value="abc"></add>
3
<add value="def"></add>
4
<add value="ghi"></add>
5
</div>