Skip to main content

Form Submit / Events

Forms / Controls: Form Submit / Events


What happens when we submit a form in the browser?

View Answer:
Interview Response: When a form is submitted, the submit event is triggered. It often gets used to verify the form (form validation) before sending it to the server or cancel the submission and process it in JavaScript. The function form.submit() enables us to transmit forms from JavaScript, and we may use it to construct and send forms to the server dynamically.

What are the two ways to allow a user to submit a form?

View Answer:
Interview Response: There are two primary methods for submitting a form. The first step is to select either ‹input type="submit"› or ‹input type="image"›. The second method is to enter data into an input field by pressing Enter. Both activities result in the form's submit-event. The handler can inspect the data, display them, and call events if any issues appear. If you use `preventDefault()`, the form does not transmit to the server.

Code Example:

<form onsubmit="alert('submit!');return false">
First: Enter in the input field <input type="text" value="text" /><br />
Second: Click "submit": <input type="submit" value="Submit" />
</form>

What is the relation between submit and click?

View Answer:
Interview Response: When a form gets sent using Enter on an input field, a click event triggers on the ‹input type="submit"›. That is rather funny because there was no click at all.

Code Example:

<form onsubmit="return false">
<input type="text" size="30" value="Focus here and press enter" />
<input type="submit" value="Submit" onclick="alert('click')" />
</form>