CodePark/ questions / Forms & inputs
START PRACTICINGPRACTICE
Frontend question · CSS & HTML › Forms & inputs

What happens when the user clicks "Show password"?

<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 submit

togglePw() 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.