Jquery Toggle If statement

Total Post:46

Points:324
 1598  View(s)
Ratings:
Rate this:

I'm trying to run a function when the state of the toggle is only "open". Right now the function runs either way. I'm not entirely sure how to make it so that it only runs when the toggle is opened. I found the toggle code somewhere and used it, it works great.

$( "#SB2p" ).click(function() {
        $( "#SB2").toggle('slow',function(){
            var $link =$("#SB2p");
            $(this).is(":visible") ? $link.text("<<<") : $link.text(">>>");
        });
        accumulative();
    });

Any help would be great.

  1. Post:194

    Points:1396
    Re: Jquery Toggle If statement

    You already have a working example. The ternary operator uses $(this).is(':visible'), so use that:

       $( "#SB2p").click(function() {
            $( "#SB2").toggle('slow',function(){
                var $link =$("#SB2p");
                if ($(this).is(":visible")) {
                  $link.text("<<<");
                  accumulative();
                } else {
                  $link.text(">>>");
                }
            });      
        });

      Modified On Apr-07-2018 12:46:45 AM

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!