hide show pure javascript code example
Example 1: javascript hide show div
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#hide").click(function(){
$("#1").hide();
$("#2").show();
});
$("#show").click(function(){
$("#1").show();
$("#2").hide();
});
});
</script>
</head>
<body>
<p id="1">Area 1</p>
<p id="2" style="display: none;">Second area</p>
<button id="show">Area 1</button>
<button id="hide">Second Area</button>
</body>
</html>
Example 2: hide automatically show and hide javascript
function toggleClock() {
// get the clock
var myClock = document.getElementById('clock');
// get the current value of the clock's display property
var displaySetting = myClock.style.display;
// also get the clock button, so we can change what it says
var clockButton = document.getElementById('clockButton');
// now toggle the clock and the button text, depending on current state
if (displaySetting == 'block') {
// clock is visible. hide it
myClock.style.display = 'none';
// change button text
clockButton.innerHTML = 'Show clock';
}
else {
// clock is hidden. show it
myClock.style.display = 'block';
// change button text
clockButton.innerHTML = 'Hide clock';
}
}