JavaScript
x
12
12
1
function w3_open() {
2
document.getElementById("mySidebar").style.display = "block";
3
document.getElementById("w3-button").style.display = "none"
4
document.getElementById("w3-buttondos").style.display ="inline-block"
5
6
}
7
8
function w3_close() {
9
document.getElementById("mySidebar").style.display = "none";
10
document.getElementById("w3-buttondos ").style.display = "none"
11
document.getElementById("w3-button").style.display ="inline-block"
12
}
JavaScript
1
2
1
<button class="w3-button w3-teal" onclick="w3_open()">☰</button>
2
<button class="w3-buttondos w3-teal" onclick="w3_close()">✖</button>
it says me -> Uncaught TypeError: document.getElementById(…) is null
Advertisement
Answer
document.getElementById
does not target a node by its class=""
but by its id=""
JavaScript
1
2
1
<button id="w3-button" class="w3-button w3-teal" onclick="w3_open()">☰</button>
2
To target an element by its class
you can use document.querySelector
JavaScript
1
2
1
document.querySelector(".w3-button")
2