Detect if an element is hidden or visible in jQuery-javascript?


I am trying to check if Ads are visible or not(blocked) when user loads my web-application page, if not show a message, which is already hidden by default,so how can I detect if an element is visible in the current page after loading it, and if is not-visible make it visible(some other div to show user's a message to load ads on my website and support us) using jQuery or javascript?

Also is it possible to toggle the visibility of an element, using the function toggle() or any other function?

Thanks


Asked by:- manish
1
: 2922 At:- 8/29/2017 8:09:52 AM
javascript jQuery visibility







2 Answers
profileImage Answered by:- vikas_jk

I am not sure if you approach about ads is good or not, but to detect if an element is visible or not using jquery you can use the code below

$(element).is(":visible"); 

Or if there is any class of element which you want to test then you can apply this code

$(".Class").each(function() {
    if ($(this).css("visibility") == "hidden") {
        // code to handle non-visible state
    } else {
        // code to handle visible state
    }
});

You can use any of the above methods

About changing the state of element, yes after applying the above code, you can easily change the state, as per your needs using .show() or .hide()

1
At:- 8/29/2017 3:12:17 PM Updated at:- 12/24/2022 8:45:22 AM


profileImage Answered by:- jon

You can also check if element is hidden or visible using below jQuery code

$('element:hidden')
$('element:visible')

Example, if we are trying to check HTML element with id "checkme"

if($('#checkme:hidden')){
   //element hiddent
}

if($('#checkme:visible')){
   //element visible
}

Thanks

0
At:- 12/24/2022 3:18:05 PM






Login/Register to answer
Or
Register directly by posting answer/details

Full Name *

Email *




By posting your answer you agree on privacy policy & terms of use