Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if DIV is visible using javascript

i needed to hide a div on my code behind:

bool hideDiv = false
//codes to change hideDiv
myDiv.visible = hideDiv;

and i want to check visibility of my div using javascript:

if (jQuery("myDiv") != null){
    //some codes
}
else{
    //some codes
}

and the 'jQuery("myDiv")' is always not null (even if the div was actually not visible), what is the better way to check if a div is visible?

like image 483
Eytch Avatar asked Dec 08 '22 15:12

Eytch


1 Answers

You can use :visible selector inside is filtering function:

if ($('#myDiv').is(':visible'))

Notes:

  • You probably forgot the # before the id in your selector(jQuery("myDiv")).
  • jQuery will never return null no matter if the searched elements exist or not, unlike document.getElementById
like image 68
gdoron is supporting Monica Avatar answered Dec 28 '22 14:12

gdoron is supporting Monica