Can I use .show () or .hide () with the 1/0 switch instead of the If statement

I'm just wondering if something like this can be done in jQuery:

$('#MyDiv').show((MyVar==2?1:0)); // 1=show, 0=hide.

otherwise I would have to write it like this everywhere.

if(MyVar==2?1:0){$('#MyVar').show();}else{$('#MyVar').hide();}
+3
source share
2 answers

There is a built-in function .toggle(boolean value)since jQuery 1.3:

$('#MyDiv').toggle(MyVar === 2);
+7
source

Given that the manual for .show () says that it takes no arguments, I believe that you should do this in the 2nd way.

- if (MyVal==2) { $('#MyVar').show(); } else { $('#MyVar').hide(); }.? , myVal == 2 true (1) false (0). , , , .

: lol ... .

0

All Articles