Eytch
Eytch

Reputation: 743

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?

Upvotes: 4

Views: 23594

Answers (1)

gdoron
gdoron

Reputation: 150243

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

Upvotes: 14

Related Questions