1

I am trying to hide a div using asp.net checkbox server control. I added a onclick javascript code for the server control but it seems not working....any suggestions?

    function hideDiv(obj) {

        if (obj.checked==true) {
            document.getElementById("divMap").style.visibility=true
        }
        else {
            document.getElementById("divMap").style.visibility = false
        }
    }

5 Answers 5

2
function hideDiv(obj) {
    if (obj.checked==true) {
        document.getElementById("divMap").style.display='block';
    }
    else {
        document.getElementById("divMap").style.display = 'none';
    }
}

or you can use jquery

function hideDiv(obj) {
    if (obj.checked) {
        $("#divMap").show();
    }
    else {
        $("#divMap").hide();
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

how can put it in asp.net? <asp:CheckBox ID="chkShowAllOnMap" Text="Show all results on map" runat="server" AutoPostBack="true" onClick="hideDiv(this)" />
2

This could also be achieved server side.

Markup:

<asp:CheckBox id="chk" runat="server" AutoPostBack="true" />
<div id="div" runat="server"></div>

C#:

protected void chk_CheckedChanged(object sender, System.EventArgs e)
{
    switch ((sender.checked)) {
        case true:
            div.Visible = false;
            break;
        case false:
            div.Visible = true;
            break;
    }
}

Comments

0

Try to use the below code instead of

document.getElementById("divMap").style.visibility = false
document.getElementById("divMap").style.visibility=true  




document.getElementById("divMap").style.display='none'
     document.getElementById("divMap").style.display='block'

Comments

0

To access and use a server side control in asp.net you have to use it's client id (ClientID) property.

function hideDiv(obj) {

    if (obj.checked==true) {
        document.getElementById("<%=YourControlID.ClientID %>").style.visibility=true
    }
    else {
        document.getElementById("<%=YourControlID.ClientID %>").style.visibility = false
    }
}

Comments

0

Perform operation on server control from javascript user “<%=control Name .ClientID%>”

 function hideDiv(obj) {

        if (obj.checked==true) {
            document.getElementById('<%= divMap.ClientID%>').style.display = 'Block';
        }
        else {
            document.getElementById('<%= divMap.ClientID%>').style.display = 'None';
        }
    }

Comments

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.