How to create toggle hide / show an HTML element in JavaScript

1 Answer

0 votes
<!DOCTYPE html>
<html>
<head>
</head>
<body>

<button onclick="myFunction()">Click to toggle</button>

<div id="divid">
Show Hide Text
</div>
<script>
function myFunction() {
    var id = document.getElementById("divid");
    if (id.style.display === "none") {
        id.style.display = "block";
    } else {
        id.style.display = "none";
    }
}
</script>
</body>
</html>

 



answered May 29, 2018 by avibootz

Related questions

2 answers 307 views
1 answer 186 views
1 answer 181 views
2 answers 7,155 views
1 answer 256 views
...