0

I want to value an array with js based on the value of an element on the web page which is an array. How can this be done?

aa=JSON.stringify(document.querySelector("#a01").innerHTML)
console.log(aa[0])
<div id="a01">[[1,2],[1,3],[1,4]]</div>
Nima Azii
  • 47
  • 5

1 Answers1

1

The content of the element is already string. You should parse the jsonString using JSON.parse() to make that an array:

var aa = JSON.parse(document.querySelector("#a01").innerHTML)
console.log(aa[0])
<div id="a01">[[1,2],[1,3],[1,4]]</div>
Mamun
  • 66,969
  • 9
  • 47
  • 59