Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using JQuery to toggle div visibility

I'm writing some jquery code to toggle a Div visibility. I followed the method posted here: http://jsfiddle.net/Ga7Jv/.

When the user clicks an image (next to a H2 tag) i want it to toggle the div.

Here is the Jquery:

$(function()
{
$(".expander").live('click', function(){
        $(this).next("#TableData").slideDown();
        $(this).removeClass('expander');
        $(this).addClass('expanded');
});

$(".expanded").live('click', function(){
        $(this).next("#TableData").slideUp();
        $(this).removeClass('expanded');
        $(this).addClass('expander');
});

Here is the HTML:

<h3><img src="toggle.png" class ="expander" alt="Expand"/> 
Tabular Data</h3>
<div id="TableData">
//Content
</div>

The is css applied to the class expander and when i click the button it appears that the css changes, as i would expect. So i assume the code is finding the toggle button and switching out the classes alright.

However it doesn't perform the action i need, which is to slide the div up or down depending on that class.

The other way i've tried to achieve this is with this:

  $(function(){
     $('.expander').live('click',function(){
     $('#TableData').show();
     $('#TableData').hide();
        });

This only closes the div and does not open again when i click it. I've also had it so it closes fine but when i open it, it immediately closes again.

Thanks

like image 574
Hulaz Avatar asked Feb 05 '14 17:02

Hulaz


People also ask

How do I toggle Show hide in jQuery?

The toggle() method toggles between hide() and show() for the selected elements. This method checks the selected elements for visibility. show() is run if an element is hidden. hide() is run if an element is visible - This creates a toggle effect.

Is Div visible jQuery?

You can use .is(':visible') selects all elements that are visible.

How do I show and hide a div on click?

To show and hide div on mouse click using jQuery, use the toggle() method. On mouse click, the div is visible and on again clicking the div, it hides.

What does toggle do in jQuery?

jQuery toggle() Method The toggle() method attaches two or more functions to toggle between for the click event for the selected elements. When clicking on an element, the first specified function fires, when clicking again, the second function fires, and so on.


1 Answers

Problem with your code is that its last function call is .hide(), thus it will always hide the div

Simply use .toggle(), it display or hide the matched elements.

$(function() {
  $('.expander').on('click', function() {
    $('#TableData').toggle();
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<h3><img src="toggle.png" class="expander" alt="Expand" />
</h3>

<div id="TableData">The Core Knowledge Sequence UK is a guideline of the fundamental skills and content to cover in English, maths, history and geography, science, music and visual arts.</div>
<p class="traversing"></p>

OR

$(function () {
    $('.expander').on('click', function () {
        $('#TableData').slideToggle();
    });
});

You can use .slideToggle(), if you want to display or hide the matched elements with a sliding motion

$(function() {
  $('.expander').on('click', function() {
    $('#TableData').slideToggle();
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>

<h3><img src="toggle.png" class="expander" alt="Expand" />
</h3>

<div id="TableData">The Core Knowledge Sequence UK is a guideline of the fundamental skills and content to cover in English, maths, history and geography, science, music and visual arts.</div>
<p class="traversing"></p>
like image 112
Satpal Avatar answered Oct 06 '22 04:10

Satpal