Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

prevent refresh of page when button inside form clicked

Add type="button" to the button.

<button name="data" type="button" onclick="getData()">Click</button>

The default value of type for a button is submit, which self posts the form in your case and makes it look like a refresh.


Let getData() return false. This will fix it.

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

All you need to do is put a type tag and make the type button.

<button id="btnId" type="button">Hide/Show</button>

That solves the issue


The problem is that it triggers the form submission. If you make the getData function return false then it should stop the form from submitting.

Alternatively, you could also use the preventDefault method of the event object:

function getData(e) {
    e.preventDefault();
}