Help me optimize the if else in JavaScript (jQuery) -


Is it possible to shorten this code in some way:

  var i = GetStringFromServer ('/ url'); If (i) {$ ('# div1'). Hide (); $ ('# DIV2') Show (). } And {$ ('# div1) .show (); $ ('# DIV2) .hide (); }  

In C # I will do just that:

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

Is it possible to erase logic in JavaScript?

Update: Thanks for the good answer, I did toggle before asking myself, but what was confused was the method sign:

  toggle (fn1, fn2);  

I thought the function was expected to have some difficult callbacks, but apparently it was flexible enough to handle both plain boolean and callback.

UPDATE2: Thanks to the comments of Robert and Fabian, the truth finally got the answer. Toggles will always make visible or invisible on the basis of the logic of the bill.

If you toggle the boolean argument, this will apply to each matching element from the docs: < / P>

Toggle the set of all the elements matched based on the switch (all shows all the elements, hides all false elements).

In your case, you want:

  $ ("# div1"). Toggle (! I); Turn on $ ("# DIV2") (i).  

Comments

Popular posts from this blog

c# - How to capture HTTP packet with SharpPcap -

jquery - SimpleModal Confirm fails to submit form -

php - Multiple Select with Explode: only returns the word "Array" -