Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery show hide element according to boolean value boolean

I use those rows to show hide some elements:

function isActive()
{
     $("#id1").hide();
     $("#id2").show();
}

But I need to change the rows above to make elements dispayed or hide according to bool value:

function isActive(toShow)
{
     $("#id1").hide();
     $("#id2").show();
}

what is the best way to implemnt it?

like image 470
Michael Avatar asked Jul 23 '26 05:07

Michael


1 Answers

Use $.fn.toggle(Boolean: display):

function isActive(toShow)
{
     $("#id1").toggle(!toShow);  // Hide when toShow = true, show when toShow = false
     $("#id2").toggle(!!toShow); // Hide when toShow = false, show when toShow = true
}

It is however important to make sure toShow is a boolean.

like image 145
Andreas Louv Avatar answered Jul 25 '26 18:07

Andreas Louv