Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting element visiblity with jQuery

Using jQuery, is there some property on an element that I can set to true/false to control the visibility of that element?

Basically, I need something like this:

$(this).visible(someCondition);

toggle() won't work because I need to be able to tell it whether or not it will be shown.

show() and hide() work, but I have to do this:

if (someCondition) {
    $(this).show();
}
else {
    $(this).hide();
}

Which, as you can see, is not nearly as elegant as the solution I'm looking for.

like image 412
Jerad Rose Avatar asked Jun 25 '26 05:06

Jerad Rose


2 Answers

toggle(someCondition) will work.
It takes an optional boolean parameter.

like image 81
SLaks Avatar answered Jun 26 '26 19:06

SLaks


As SLak suggested, toggle() works. If you are using classes that have visibility states set (i.e., display: none or visibility:hidden), you can also use toggleClass().

like image 37
mitch Avatar answered Jun 26 '26 19:06

mitch