JS | Call on an element by variable | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

JS | Call on an element by variable

so i'm working on the base defense challenge and i'm trying to figure out how you can call on an html element that has an element of lets say s02 and you have a variable that is s02. when you do document.getElementById()can you put the variable in the () i've tried doing it but it doesn't seem to work please help. https://code.sololearn.com/Wn5HWZDKndS5/?ref=app

7th Jul 2018, 7:29 AM
Emperor Bob
Emperor Bob - avatar
2 Answers
+ 2
Try <td id="s02" data-num="s02">2</td> <script> var s02 = document.getElementById("s02"); console.log(s02.dataset.num) </script>
7th Jul 2018, 7:38 AM
Calviղ
Calviղ - avatar
+ 1
Calviղ i dont quite understand could you put in a way as to where the background color of the element will change? also the numbers are random
7th Jul 2018, 7:39 AM
Emperor Bob
Emperor Bob - avatar