JQuery UI - swap feature that hides and hides elements using a selector

Foreword: I have jQuery and jQuery UI included in a page.

I have this function defined:

    function swap(sel) {
        if (1 == 1) {
           $(sel).hide('drop',{direction:'left'});
        }
    }

      

How do I fix the (1 == 1) part to check if the element is hidden and then return it if it is. I'm sure it's easy, but I'm new to jQuery.

0


source to share


4 answers


if you don't like toggle

it this might help you:



function swap(sel) {
  if($(sel).is(':visible')) {
    $(sel).hide('drop',{direction:'left'});
  } else {
    $(sel).show('drop',{direction:'left'});
  }
}

      

+3


source


Why aren't you using toggle () ?

For example:



function swap(sel) {
  $(sel).toggle();
} 

      

+2


source


Perhaps $(sel).toggle();

this is what you are looking for? This is the best way to toggle the visibility of elements.

+2


source


As other respondents said, toggle()

is the best solution. However, if for some reason you can't / don't want to use a switch, and if your selector is only for one element:

function swap(sel) {
    if ($(sel).is(':visible')) {  // Is this element visible?
       $(sel).hide('drop',{direction:'left'});
    }
}

      

Caution: . The visible check will return a false positive value if the item or any of its parents :hidden

. If this is important to you, you might want to check this solution , which also tries to test it.

+2


source







All Articles