Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery preventing postback on button click

Tags:

jquery

People also ask

How to stop postback on button click in javascript?

The postback on submit button can be avoided by giving return=false in the event handler function as below.

How to avoid postback in asp net using jQuery?

We can avoid postback using Jquery you can simply call the jquery function onclientclick property of button then you can overcome the postback and remember one thing you need to return false in that jquery function to achieve this. Give respect to your work, Instead of trying to impress your boss.

How to stop page load in jQuery?

You can use . preventDefault() method on your event. This will stop the default action of a page load when the button is clicked.


$('#btnNext').click(function(e) {
        e.preventDefault();
    });

I was working with a button and discovered that if you want to prevent the button from doing an auto-postback, you need to specify the type as "button". For example:

<button id="saveButton">Save</button> 

--this will generate an auto-postback (tested in IE)

<button type="button" id="saveButton">Save</button> 

--this will not

Hope this helps someone.


In Asp.net to prevent page postback on button click use preventDefault() function

<html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>WebForm1</title>
        <script src="js/jquery-2.2.2.js" type="text/javascript"></script>
        <script>
            $(document).ready(function () {
                $("#btnshow").click(function (e) {
                    e.preventDefault();
                    $("#Label1").show();
                });
                $("#btnhide").click(function (e) {
                    e.preventDefault();
                    $("#Label1").hide();
                })
            });
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>

        </div>
            <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
            <asp:Button ID="btnshow" runat="server" Text="Show" />
            <asp:Button ID="btnhide" runat="server" Text="Hide" />
        </form>


    </body>
    </html>