No. | Title |
---|---|
1 | Code Example 1 |
2 | Conclusion |
<!DOCTYPE html>
<html>
<head>
<title>Show/Hide Element with Checkbox</title>
</head>
<body>
<h2>Show/Hide Element</h2>
<input type="checkbox" id="myCheckbox"> Show Element
<div id="myElement" style="display: none;">
This is the element you can show or hide.
</div>
<script>
// Get the checkbox element and the element to show/hide
const checkbox = document.getElementById('myCheckbox');
const element = document.getElementById('myElement');
// Add an event listener to the checkbox
checkbox.addEventListener('change', function() {
if (checkbox.checked) {
// If the checkbox is checked, show the element
element.style.display = 'block';
} else {
// If the checkbox is unchecked, hide the element
element.style display = 'none';
}
});
</script>
</body>
</html>