web analytics

How to stop page refresh when button inside form clicked?


codeling 1579 - 6579
@2021-06-14 22:08:39


I have a button in a web page which will call a JavaScript method when clicked, but there is a side effect which is the whole page get refreshed after the button is clicked.

<form method="POST">
    <button name="data" onclick="getData()">Click</button>

How to stop the page refresh when the button is clicked?

@2021-06-14 22:13:34

To fix this issue, you should let the Javascript function getData() return false.

<script type="text/javascript>
 function getData(){
    //do something
    return false;
<form method="POST">
    <button name="data" onclick="javascript:return getData()">Click</button>




You must Sign In to comment on this topic.

© 2023 Digcode.com