How can I change CSS display none or block property using jQuery?
#1
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:

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?
Reply
#2
You've got the basic idea down, but there might be a couple of things you can improve. First, jQuery offers a convenient `.toggle()` method which automatically switches between showing and hiding the matched elements. This method handles the conditional logic you've written manually, so you can simplify your JavaScript code significantly.
Here's an example of how you could modify your code to use the `.toggle()` method:

Code:
$('#toggleButton').click(function() {
        $('#myDiv').toggle();
    });
});

If you specifically need to toggle between `display: none` and `display: block`, you could also use the `.css()` method like this:

Code:
$('#toggleButton').click(function() {
        var $myDiv = $('#myDiv');
        $myDiv.css('display', $myDiv.css('display') === 'none' ? 'block' : 'none');
    });
});

Make sure that your HTML elements' IDs match what you're using in your jQuery selectors. If the IDs don't match, the script won't work.
Now, if you want to see the final working code, here it is with the necessary library included so you can run it directly:

Code:
<!DOCTYPE html >
    <html lang = "en" >
    <head >
    <meta charset = "UTF-8" >
    <meta name = "viewport"
content = "width=device-width, initial-scale=1.0" >
    <title > Toggle Display Example < /title> <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js" > < /script> <script >
    $(document).ready(function() {
        $('#toggleButton').click(function() {
            $('#myDiv').toggle();
        });
    });
</script>
</head> <body >
    <button id = "toggleButton" > Toggle Div < /button> <div id = "myDiv"
style = "display:none;" >
    This is my div.
</div>
</body>
</html>

Ensure that your HTML has an element with the id "myDiv" and a button with the id "toggleButton" for the jQuery script to work. Also, check for typos and confirm the jQuery script is running after the DOM is fully loaded.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)