3

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?

404 Not Found
  • 1,223
  • 2
  • 22
  • 31

2 Answers2

6

Try this,

$(document).ready(function(){
    var neck='';
    var chest='';
    $("#neck,#chest").click(function(){
        if($(this).toggleClass('opa').hasClass('opa')){
           neck = "neck";
        }else{
           neck = "";
        }
    });
});  
Kemal Dağ
  • 2,743
  • 21
  • 27
  • 1
    thanks it works like charm... but $(this).attr("neck","neck"); stmt is not storing data in var. so instead of that i am manually assigning value like **neck="neck";**. and it's working. is it good way? – 404 Not Found Sep 05 '13 at 06:30
  • Sorry, I did not get what you really want, using .attr method completely useless in your case since you want to assign to a variable, I edited the code. Thanks for pointing that out. – Kemal Dağ Sep 05 '13 at 06:33
0

You should read. This is the way to set boolean values in toggle. Where you can simple pass your neck variable with desired value.

http://api.jquery.com/toggle/#toggle-showOrHide

Pallab
  • 285
  • 1
  • 7
  • @MarsOne does that necessarily means that he has been able to understand everything written over there? – Pallab Sep 05 '13 at 06:18
  • 1
    Fair enough, you could still post this as a comment. In what way does this qualify as an answer? – MarsOne Sep 05 '13 at 06:20
  • @Pallab i have already checked there and tried different examples which are given there. – 404 Not Found Sep 05 '13 at 06:22
  • I do have a slight suggestion about passing his variable with the desired value. Well I agree this could have been a comment as well. But just because it is an answer it should not be down voted. Down vote if the answer is wrong. – Pallab Sep 05 '13 at 06:24
  • 2
    *"Down vote if the answer is wrong"*. Downvoting an answer does not depend on answer being wrong/right ONLY. My reason is basic reason which SO follows. Its not useful – MarsOne Sep 05 '13 at 06:28