Ja va Ja va - 1 month ago 20
ASP.NET (C#) Question

Checking Div Visibility in ASP.NET using Javascript

How do you check if a div's visibility is hidden using JavaScript and ASP.Net?

If you look below, I am using an "if statement" to return an Alert("hi") if the div is not visible. Yet, the code is not working.

Any help is appreciated, thank you. Language is C#/JavaScript

<script>
function emptyRunReportConfirmation() {
var divDateFiltersID = document.getElementById('<%=divDateFilters.ClientID%>');

if (divDateFiltersID.style.visibility == "hidden") {
alert("hi");
}
}
</script>

<!-- this is a button to call the function -->
<asp:Button ID="buttCustomerFilt" runat="server" class="btn btn-primary" ClientIDMode="Static" Text="Run Report" OnClientClick="if ( ! emptyRunReportConfirmation()) return false;" OnClick="buttCustomerFilt_Click" /></div>

<!-- this is the div to check if visible -->
<div runat="server" id="divDateFilters" visible="false"></div>

Answer

Setting visible="false" on a runat="server" element will remove the element entirely from the page. The DIV element will not render at all. You could check your HTML to verify this.

It depends a bit on what you want to do, but in this case if you use visible="false" you can check if the variable is empty to see if the element exists.

if (!divDateFiltersID) {
    alert("hi");
}
Comments