2

I have the following piece of code, I need to show or hide the panels. My question is, the show() works on alternative clicks only.

var allPanels = $(".panel");
allPanels.hide(2000);
allPanels.show();

I would like to know the reason why show() does not work consistently.

3
  • I figured that if I use allPanels.hide(2000); and allPanels.show(1000); or allPanels.show(2000); it works but not if allPanels.show(); Commented Apr 22, 2017 at 9:28
  • You're using animation so you can shot after they are hidden allPanels.hide(2000, function() { $(this).show() }); Commented Apr 22, 2017 at 9:30
  • 1
    Thanks Alon and @Rana - I guess I was not clear enough. I wanted to know why show() was not able to display the elements unless show(1000) was called. Nevertheless, thanks for your reply. Commented Apr 22, 2017 at 10:12

3 Answers 3

1

That's not working because you set time in .hide(2000). You have to write in following way::

HTML

<div class="panel" >This is a DIV</div>
<button type="button" class="buttonClick" >Click me</button>

JQUERY

$(document).ready(function (e) {
    $(document).on('click', '.buttonClick', function () {
        var allPanels = $(".panel");
        allPanels.hide(2000, function(){
            allPanels.show();
        });
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

@Sujit Please accept the answer if it is right for helping others.
0

an easier approach would be to .stop() the current animation just before you start another one.

var allPanels = $(".panel");
allPanels.hide(2000);
allPanels.stop().show();

this way you won't have to place the code in the callback. do notice though, doing this will explicitly pause the animation ignoring its current state.

1 Comment

allPanels.stop().show(); did not work, threw an exception.
0

When I removed the animation timing from the

var allPanels = $(".panel");
allPanels.hide(2000);
allPanels.stop().show();

Changed to

var allPanels = $(".panel");
allPanels.hide();
allPanels.stop().show();`enter code here`

it started working. Hide and Show works seamlessly. Thanks. But can anyone say why hide and show with timing does not work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.