web developing
8 Jul
That was described in my recent post and it describes the case when you have a particular element in the DOM tree, or if that element does not exists.
example:
HTML:
<div id="holder">
<div id="element_id">Hello World!</div>
</div>
JavaScript (jQuery):
console.log( $('#element_id').length )
this prints 1, because the element exists. In the following example is different.
example:
HTML:
<div id="holder">
</div>
JavaScript (jQuery):
console.log( $('#element_id').length )
that returns 0, because obviously the element does not exists.
Well than the HTML look like that:
example:
<div id="holder">
<div id="element_id" style="display:none">
Hello World!
</div>
</div>
The solution is a bit different, but simple
jQuery code:
if ($('#element_id:visible').length > 0)
console.log('the element is visible');
else
console.log('the element is not visible');
Related posts:
3 Responses for "jQuery check for element visibility"
Hi Good day,
Thanks! this was really helpful & simple to integrate. Thanks, keep up the good work!
Fantastic code in just two lines !!! Great job
Nice example works perfectly
Leave a reply