Ccna final exam - java, php, javascript, ios, cshap all in one. This is a collaboratively edited question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.
Thursday, May 24, 2012
How do you test if something is hidden with jQuery?
In jQuery, suppose you have an element of some kind that you're hiding and showing, using .hide() , .show() or .toggle() . How do you test to see if that element is currently hidden or visible on the screen?
None of these answers address what I understand to be the question, which is what I was searching for, "how do I handle items that have visibility == hidden?". Neither :visible nor :hidden will handle this, as they are both looking for display per the documentation. As far as I could determine, there is no selector to handle CSS visibility. Here is how I resolved it (standard jQuery selectors, there may be a more condensed syntax):
$(".item").each(function() { if ($(this).css("visibility") == "hidden") { // handle non visible state } else { // handle visible state } })
If you already have a reference to a particular element and you want to perform some action on it only if it is visible, or only if it is hidden then you can do do the following. This basically allows you to do the following, but without the 'if' statement :
if (weWantToHideTheButton) { // hide button by sliding to left $(button).filter(":visible").animate({ width: "toggle" }); } else { // show button by sliding to right $(button).filter(":hidden").animate({ width: "toggle" }); }
This uses the same :visible or :hidden check, but acts on a specific element we already have previously selected (the variable button).
In this case I wanted to do this, but in only one line.
I use css class .hide { display: none!important; }, for hiding/showing I call .addClass("hide")/.removeClass("hide"), for checking visibility i use .hasClass("hide"). It's simple and clear way to check/hide/show elements, if you don't plan to use .toggle() or .animate() methods.
Element could be hidden with "display:none", "visibility:hidden" or "opacity:0". Difference between those methods:
display:none hides the element and it does not take up any space; visibility:hidden hides the element, but it still takes up space in the layout; opacity:0 hides the element as "visibility:hidden" and it still takes up space in the layout; the only difference is that opacity let to do element partly transparent;
How element visibility and jQuery works;
if ($('.target').is(':hidden')) { $('.target').show(); } else { $('.target').hide(); }
if ($('.target').is(':visible')) { $('.target').hide(); } else { $('.target').show(); }
Another answer you should put into consideration is if you are hiding an element, you should use jquery, but instead of actually hiding it, you remove the whole element but you copy its html content and the tag itself into a jquery variable, and then all you need to do is test if there is such a tag on the screen, using the normal if ($('#thetagname')).
As, the question refers to a single element, this code might be more suitable:
ReplyDelete$(element).is(":visible") // Checks for display:[none|block], ignores visible:[true|false]
Same as twernt's suggestion, but applied to a single element.
You can use the "hidden" and "visible" selectors.
ReplyDelete$('element:hidden')
http://docs.jquery.com/Selectors/hidden
$('element:visible')
http://docs.jquery.com/Selectors/visible
$(element).css('display') == 'none'
ReplyDeleteFunctions don't work with visibility attribute.
Tsvetomir's solution worked for me, couldn't post a comment though. Expanding on it...
ReplyDeleteif( $(element).is(":visible") ) {
// element is visible
}
else {
// element is not visible
}
None of these answers address what I understand to be the question, which is what I was searching for, "how do I handle items that have visibility == hidden?". Neither :visible nor :hidden will handle this, as they are both looking for display per the documentation. As far as I could determine, there is no selector to handle CSS visibility. Here is how I resolved it (standard jQuery selectors, there may be a more condensed syntax):
ReplyDelete$(".item").each(function()
{
if ($(this).css("visibility") == "hidden")
{
// handle non visible state
}
else
{
// handle visible state
}
})
From: http://docs.jquery.com/Frequently_Asked_Questions#How_do_I_determine_the_state_of_a_toggled_element.3F
ReplyDeleteYou can determine whether an element is collapsed or not by using the :visible and :hidden selectors.
var isVisible = $('#myDiv').is(':visible');
var isHidden = $('#myDiv').is(':hidden');
If you're simply acting on an element based on its visibility, just include ":visible" or ":hidden" in the selector expression. For example:
$('#myDiv:visible').animate({left: '+=200px'}, 'slow');
It's worth mentioning (even after all this time), that $(element).is(":visible") works for jQuery 1.4.4, but not for jQuery 1.3.2, under IE8.
ReplyDeleteThis can be tested using Tsvetomirs helpful test snippet. Just remember to change the version of jQuery, to test under each one.
If you already have a reference to a particular element and you want to perform some action on it only if it is visible, or only if it is hidden then you can do do the following. This basically allows you to do the following, but without the 'if' statement :
ReplyDeleteif ($(button).is(":visible")) {
$(button).animate({ width: "toggle" }); // hide button
}
Here's how to do it without the 'if' :
var button = $('#btnUpdate')[0];
if (weWantToHideTheButton)
{
// hide button by sliding to left
$(button).filter(":visible").animate({ width: "toggle" });
}
else {
// show button by sliding to right
$(button).filter(":hidden").animate({ width: "toggle" });
}
This uses the same :visible or :hidden check, but acts on a specific element we already have previously selected (the variable button).
In this case I wanted to do this, but in only one line.
This works for me, and I am using show() and hide() to make my div hidden/visible
ReplyDeleteif( $(this).css("display") == 'none' ){
/* your code here*/
}
else{
/* alternate logic */
}
I use css class .hide { display: none!important; }, for hiding/showing I call .addClass("hide")/.removeClass("hide"), for checking visibility i use .hasClass("hide").
ReplyDeleteIt's simple and clear way to check/hide/show elements, if you don't plan to use .toggle() or .animate() methods.
Element could be hidden with "display:none", "visibility:hidden" or "opacity:0". Difference between those methods:
ReplyDeletedisplay:none hides the element and it does not take up any space;
visibility:hidden hides the element, but it still takes up space in the layout;
opacity:0 hides the element as "visibility:hidden" and it still takes up space in the layout; the only difference is that opacity let to do element partly transparent;
How element visibility and jQuery works;
if ($('.target').is(':hidden')) {
$('.target').show();
} else {
$('.target').hide();
}
if ($('.target').is(':visible')) {
$('.target').hide();
} else {
$('.target').show();
}
if ($('.target-visibility').css('visibility') == 'hidden'){
$('.target-visibility').css({visibility: "visible", display: ""});
}else{
$('.target-visibility').css({visibility: "hidden", display: ""});
}
if ($('.target-visibility').css('opacity') == "0"){
$('.target-visibility').css({opacity: "1", display: ""});
}else{
$('.target-visibility').css({opacity: "0", display: ""});
}
Useful jQuery toggle methods:
$('.click').click(function() {
$('.target').toggle();
});
$('.click').click(function() {
$('.target').slideToggle();
});
$('.click').click(function() {
$('.target').fadeToggle();
});
Another answer you should put into consideration is if you are hiding an element, you should use jquery, but instead of actually hiding it, you remove the whole element but you copy its html content and the tag itself into a jquery variable, and then all you need to do is test if there is such a tag on the screen, using the normal if ($('#thetagname')).
ReplyDeleteOne Can simply use hidden or visible attribute like
ReplyDelete$('element:hidden')
$('element:visible')
or You can simplfy the same with IS as following:-
$(element).is(":visible")