Gopal
Gopal

Reputation: 217

how to hide div with Class/id with jQuery

I have a function openbag(); for opening div. this div is also open on click and addClass "active". but i am open this div from server side, call the function openbag();. but the problem is this div is open every time when refresh the other page. I have removed "active" class and check if class has not but this is always showing. here is my code i am using.

function openBag() {
   $("#bag").addClass("active");
   $("#expanding-bag").slideDown("fast");
  }


$(document).ready(function () {
    if ($("div#bag").hasClass("active")) {
        $("#expanding-bag").show();
        $("#bag").removeClass("active");
    }

    else {
        $("#expanding-bag").hide();
    }

in c# :

Page.ClientScript.RegisterStartupScript(typeof(Page), "alert", "<script language='javascript'>openBag();</script>");

Upvotes: 0

Views: 658

Answers (2)

Vishal Gavle
Vishal Gavle

Reputation: 367

you can also try this way put your

expandingbag to runat="server"

access your expanding-bag on server side and when you have to hide the control use following code

expandingbag.Style.add("display","none");

or when you have to display then use

expandingbag.Style.add("display","inherit");

Upvotes: 1

Aristos
Aristos

Reputation: 66641

You can avoid open it and close it and all that, and direct manipulate the javascript from code behind. One trick is to use Literal and show it or not, and place inside of it the code as:

<asp:Literal runat="server" id="ShowIt" Visible="false" EnableViewState="false">
$(document).ready(function () {
    $("#expanding-bag").show();    
}
</asp:Literal>

<asp:Literal runat="server" id="HideIt" Visible="false" EnableViewState="false">
$(document).ready(function () {
    $("#expanding-bag").hide();
}
</asp:Literal>

and on code behind you make ShowIt.Visible=true; in the one you wish to run.

Other way is to direct render this code on page from code behind.

Upvotes: 2

Related Questions