By default, when you click on a <form> field with a <input type="submit">, by default, the webpage will try to make a request to the currentURL, and since it is the currentURL your page will refresh. To remove the default behavior of the input type = "submit", you would need to add some JavaScript to the button that is something called preventDefault.
The preventDefault() method cancels the event if it is cancelable, meaning that the default action that belongs to the event will not occur.
For example, this can be useful when:
- Clicking on a "Submit" button, prevent it from submitting a form
- Clicking on a link, prevent the link from following the URL
// https://jsfiddle.net/5f6n9wL1/2/
<form>
<input type="submit" id="submit">
</form>
<script>
// VanillaJS + DOMContentLoaded
document.addEventListener("DOMContentLoaded", function() {
document.getElementById('submit').addEventListener('click', function(event){
event.preventDefault();
alert('run business logic Vanilla');
});
});
//Jquery + DOMContentLoaded
$(function() {
$('#submit').on('click', function(event){
event.preventDefault();
alert('run business logic Jquery');
});
});
</script>