views:

160

answers:

7

Is it possible to somehow shorten this code:

var i=GetStringFromServer('/url');
if(i){
   $('#Div1').hide();
   $('#Div2').show();
}
else{
   $('#Div1).show();
   $('#Div2).hide();
}

In C# I'd simply do this:

bool smth=GetBool();
_el1.Visible=smth;
_el2.Visible=!smth;

Is it possible to mimick the logic in JavaScript?

UPDATE: Thank you guys for nice answers, I peeked at toggle myself before asking but what confused was the method signature:

toggle(fn1, fn2);

I thought that function expected some tricky callbacks but apparently it's flexible enough to handle both plain booleans and callbacks.

UPDATE2: Thanks to Robert's and Fabien's comments, the true answer was finally found. Toggle will always make elements visible or invisible based on evaluating the argument to bool.

+7  A: 
$('#Div1, #Div2').toggle(i);
Darin Dimitrov
"Toggle displaying each of the set of matched elements based upon the switch (true shows all elements, false hides all elements).If the switch is false, toggle makes them hidden (using the hide method). If the switch is true, toggle makes them shown (using the show method)."
cballou
As cballou mentions, this will either hide or show BOTH divs and doesn't seem to be what Valentin wants.
Eric Lennartsson
This code will show them all, or hide them all like said in the DOM, that's not what is expected.
Fabien Ménager
The switch argument will tell what to do, so if "i" evaluates to true, they will all be shown, whatever the elements' visibility is.
Fabien Ménager
Fabien, let me check this out..
Valentin Vasiliev
Valentin, please read the first comment, from the docs (I quote the same in my answer). It explains what toggle *actually* does if you pass it a boolean value; ie if i is true, Div1 and Div2 are shown; if not, they are both hidden. This answer is wrong.
Robert Grant
Yes, I hastily marked as 'accepted' w/out checking out, but you're correct, thank you, Robert.
Valentin Vasiliev
No worries; several other people caught the error at the same time as me :) I'd be more concerned with the people who upvoted this post; presumably they only vote if they know the answer's right, so I'm rather surprised it got this many votes.
Robert Grant
+1  A: 

Have a look at this link

Jquery using toggle to show/hide a div

astander
+2  A: 
var i=GetStringFromServer('/url');

$('#Div1').toggle(!i);
$('#Div2').toggle(i);

But you may have problem to get the i var if you do it this way as it looks like you are using Ajax.

Fabien Ménager
A: 

In addition to the other answers, if you can't toggle, but need to show/hide explicitly:

var divs = ['#div1', '#div2'],
    j = (i ? 1 : 0)
$(divs[1 - j]).show()
$(divs[j]).hide()
culebrón
+4  A: 

If you give toggle a boolean argument, it will apply that to every matched element. From the docs:

Toggle displaying each of the set of matched elements based upon the switch (true shows all elements, false hides all elements).

So in your case, you want:

$("#Div1").toggle(!i);
$("#Div2").toggle(i);
Robert Grant
+1  A: 

It's probably fine as it is, but you could do this if you like:

var i=GetStringFromServer('/url');
$('#Div1')[i  ? 'hide' : 'show']();
$('#Div2')[!i ? 'hide' : 'show']();

I don't think you get much out of it in terms of space savings or runtime, and it's a lot less clear to future code maintainers.

T.J. Crowder
Yes thanks, but in this case I prefer going against readability, since I have huge sections that need to be toggled, which creates rows of uniform code difficult to read
Valentin Vasiliev
Exactly my point.
T.J. Crowder
A: 

I'm used to do like this:

  if($('#Div1').css("display") == "none")
     {
       $(this).show(); 
     }
     else
     {
       $(this).hide(); 
     }
marko