简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM