04-30-2024, 05:15 PM
I'm trying to toggle the display property of an HTML element using jQuery. I have a button that, when clicked, should either hide or show a div based on its current state. If the div is visible, clicking the button should hide it (setting its CSS display property to 'none'). Conversely, if the div is hidden, clicking the button should reveal it (setting the display property to 'block'). I've attempted to write the script, but it doesn't seem to be working correctly.
Here's what I've tried:
I ensure that I have included the jQuery library in my HTML file:
However, when I click the button, nothing happens. The div doesn't change its display state. What am I missing here?
Here's what I've tried:
Code:
$('#toggleButton').click(function() {
if ($('#myDiv').css('display') == 'none') {
$('#myDiv').css('display', 'block');
} else {
$('#myDiv').css('display', 'none');
}
});
});
I ensure that I have included the jQuery library in my HTML file:
Code:
<script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js" > < /script>
However, when I click the button, nothing happens. The div doesn't change its display state. What am I missing here?