Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to recognize toggle() status (true or false)

i am newbie in JQuery.

i want to get status of toggle() like it's true or false?

here i want to set variable when toggle gets clicked as true, and when its gets false i want to reset that variable value again.

$(document).ready(function(){
         var neck='';
         var chest='';
            $("#neck,#chest").click(function(){
                $(this).toggleClass('opa');
            })
        })

here in class opa i am just setting opacity level. if it's true then opacity 1 and if it's false then opacity 0.

This code is working fine its toggling class (adding & Removing).

but i want to set neck value as neck="neck" when it's true (clicked first time #neck) or set neck=""; when it's false (clicked second time #neck).

i referred http://api.jquery.com/toggle/ link also but i didn't find my answer.

is it possible to get status like true/false or something like this?

like image 226
404 Not Found Avatar asked Jan 26 '26 22:01

404 Not Found


1 Answers

Try this,

$(document).ready(function(){
    var neck='';
    var chest='';
    $("#neck,#chest").click(function(){
        if($(this).toggleClass('opa').hasClass('opa')){
           neck = "neck";
        }else{
           neck = "";
        }
    });
});  
like image 70
Kemal Dağ Avatar answered Jan 29 '26 11:01

Kemal Dağ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!