You are using a checkbox in a wrong way here. We must work with for checked
property instead
if(b =="on"){
document.getElementById("demo").style.visibility = "hidden"
}
b=="on"
is a truthy string and is always true.
Also, best is to use display:none;
instead of hidden
.
document.getElementById("demo").style.display= "none";
FULL WORKING CODE: Only when the input is checked and we click on the the below div should disappear (This makes more sense in this example). Try this
<input type = "checkbox" id = "on"></input>
<button onclick = "myFunction()">Dissappear.</button>
<p id = "demo">Html is the best</p>
<script>
function myFunction(){
var b = document.getElementById("on");
console.log(b);
if(b.checked){
document.getElementById("demo").style.display = "none";
}
}
</script>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…