2

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.

6 Answers 6

7
$('#Div1, #Div2').toggle(i);
Sign up to request clarification or add additional context in comments.

7 Comments

"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)."
As cballou mentions, this will either hide or show BOTH divs and doesn't seem to be what Valentin wants.
This code will show them all, or hide them all like said in the DOM, that's not what is expected.
The switch argument will tell what to do, so if "i" evaluates to true, they will all be shown, whatever the elements' visibility is.
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.
|
4

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

Comments

2
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.

Comments

1

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.

1 Comment

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
0

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

Comments

0

I'm used to do like this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.