4

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?

1 Answer 1

14

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
Sign up to request clarification or add additional context in comments.

4 Comments

hmmm, unfortunately, still the same, i've tried your code if ($('#myDiv').is(':visible')), and i've tried my code with additional "#" if (jQuery("#myDiv") != null). still as if it is always visible
@Eytch, this code is perfectly valid and works, it seems like you have problem somewhere else or that your div isn't hidden, check it with: $('#myDiv').is(':hidden')
i ended up using document.getElementById("MainContent_myDiv") :)
@Eytch, Dear lord, so your id isn't myDiv! use this: $('#MainContent_myDiv').is(':visible')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.