2

I have created a webform file with login.aspx. But when i am clicking on the button the page is getting refreshed and the javascript function is getting called instead what i want is that after clicking on the button the page should not get refreshed and should be able to invoke the javascript function?

  <html>
  <body>
  <form runat="server">
  <button id="formsubmission" onclick="myfunction()"> GetData </button>
  </form>
  </body>
  </html>
anand
  • 1,711
  • 2
  • 24
  • 59

5 Answers5

3

You have to set return false after the function call.Otherwise page gets full postback.

<button id="Button1" onclick="myfunction(); return false;"> GetData </button>

Return false prevents from page postbacks after executing the function..

You can refer the link below for reading more about return false

Return false in javascript..

Community
  • 1
  • 1
Jameem
  • 1,840
  • 3
  • 15
  • 26
2

use event.preventDefault() to stop default behaviour in myfunction().

so your function will look like:

function myfunction(event){
    event.preventDefault();
    ...
    ...
}
Yusril Maulidan Raji
  • 1,682
  • 1
  • 21
  • 46
Karthick Kumar
  • 2,349
  • 1
  • 17
  • 30
2

If you do not need page refresh simply return false after function call and make sure you do not get any error in function call.

<button id="formsubmission" onclick="myfunction(); return false;"> GetData </button>
Adil
  • 146,340
  • 25
  • 209
  • 204
1

You can use preventDefault for this.

$("#formsubmission").click(function(event){
  event.preventDefault();
  // code to execute
});
Hauke
  • 419
  • 2
  • 6
  • 18
0

If you use <button> tag , it will act as a submit button, it will post back. So use type="button" in <button> tag , it will not allow to post back , it will act like html control.

Try this:

<button type="button" id="formsubmission" onclick="myfunction()"> GetData </button>
Suganth G
  • 5,136
  • 3
  • 25
  • 44