Home > DeveloperSection > Forums > Jquery Toggle If statement
sandy taylor
sandy taylor

Total Post:46

Points:324
Posted on    November-23-2014 10:15 PM

 JQuery JavaScript 
Ratings:


 1 Reply(s)
 950  View(s)
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.



Kamlakar Singh
Kamlakar Singh

Total Post:194

Points:1396
Posted on    November-24-2014 12:10 AM

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(">>>");

            }

        });      

    });


Don't want to miss updates? Please click the below button!

Follow MindStick