+ 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
2 odpowiedzi
+ 2
Try
<td id="s02" data-num="s02">2</td>
<script>
var s02 = document.getElementById("s02");
console.log(s02.dataset.num)
</script>
+ 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