I was trying to press a button to reload the page but it doesn't work and I don't know why
I'm using handlebars, so in theory all this code down here is inside a <body>
It sends the "POST" and everything okay, the only thing that does not run is the script
<script>
const reload = document.getElementById('reload');
reload.addEventListener('click', _ => {
window.location.reload();
});
</script>
<div >
. . .
<form action="/dashboard/" method="POST">
<div >
<button id="reload" >ye!</button>
</div>
</form>
CodePudding user response:
The default type of button is "submit" which will submit the form.
for this case, you should change the button type to "button" or use preventDefault to stop the default behavior of the button clicked.
change the button type to "button"
<button type="button" id="reload" >ye!</button>
or use preventDefault
reload.addEventListener('click', e => {
e.preventDefault()
window.location.reload();
});
CodePudding user response:
Instead of using JS you can directly specify it in button.
<button onClick="window.location.reload();" >ye!</button>