<form action="/login" method="post">
<input type="password" name="pw" required>
<button onclick="togglePw()">Show password</button>
<button type="submit">Log in</button>
</form>
togglePw() runs and then the form tries to submittogglePw() runs, and then the form tries to submit. A button without a type inside a form defaults to type="submit". So after the click handler runs, the form tries to submit. Here, native validation may block it if the password is empty, because the field is required.
"Only togglePw() runs" is the tempting wrong answer. That would be true only with type="button". So add type="button" to every button that should not submit.
Read more
- <button> (MDN) (https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button)developer.mozilla.org