I would like to make the SUBMIT-BUTTON disable just after submitting. Here is what I wrote and it doesn’t work. Could you please give me a clue what the problem is?
JavaScript
x
16
16
1
<div>
2
<form id="manual_form" action="" method="POST">
3
<button id="button" type="submit" name="manual_start">SUBMIT!</button>
4
</form>
5
</div>
6
<hr>
7
<script type="text/javascript">
8
let manual_form = document.getElementById('manual_form');
9
let manual_button = document.getElementById('button');
10
11
manual_form.addEventListener('submit', (evt) => {
12
console.log('submitted!');
13
manual_button.disabled = true;
14
}, false);
15
</script>
16
Advertisement
Answer
When you click the submit
button, the page reloads. That is why you don’t see the disabled
attribute in action. You can add evt.preventDefault();
in the event Handler to prevent the reloading
JavaScript
1
8
1
let manual_form = document.getElementById('manual_form');
2
let manual_button = document.getElementById('button');
3
4
manual_form.addEventListener('submit', (evt) => {
5
evt.preventDefault();
6
console.log('submitted!');
7
manual_button.disabled = true;
8
}, false);
JavaScript
1
6
1
<div>
2
<form id="manual_form" action="" method="POST">
3
<button id="button" type="submit" name="manual_start">SUBMIT!</button>
4
</form>
5
</div>
6
<hr>