Link to home
Start Free TrialLog in
Avatar of YZlat
YZlatFlag for United States of America

asked on

JQuery works in plain html but not with ASP.NET

I have a button and div on my page. Div is not visible initially and I want to use JQuery to make it visible whenever the user clicks a button.

Below is my code that makes div invisible when the document loads and then, when the button is clicked, makes it visiable again. it works in plain html but not with ASP.NET page. On ASP.NET page, as soon as I click the button, the div appears for a second and disappears rigth away. Can someone help me fix my code?



<script type="text/javascript">
            <!--
            $(document).ready( function(){
             $('#div1').css('display', 'none');
                $('#btn1').click(function(){
                    $('#div1').css('display', 'block');
                });
            });
            //-->
            </script>
                                  1:

I tried using $('#div1').show() but it produces the same result

I also tried setting visibility to visible but to no avail
Avatar of Tom Beck
Tom Beck
Flag of United States of America image

Is your button an asp:Button? If so, asp:Buttons automatically cause a postback. When the page posts back, the div is reset to display:none by the jquery, thus the brief visibility of the div. You can use a standard input to correct the problem.

    <input id="btn1" type="button" runat="server" value="Button 1" />
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Dear Friend ,

  If U want the button post back and as well as u need to make the div visible true then use the fallowing Code


In aspx Page
_____________


function fncVisible()
{
   $('#div1').css('display', 'none');
                $('#btn1').click(function(){
                    $('#div1').css('display', 'block');
                });
}


and in .cs Page
________________

Protected void button_Click(object sender,Eventargs e)
{
//If U have used Update Pannels in Aspx
 ScriptManager.RegisterStartupScript(this, this.GetType(), "VisiblePopup", "fncVisible();", true);



}
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of YZlat

ASKER

Worked like a charm!