EN
JavaScript - submit form example
0
points
In this article, we would like to show you how to submit a form in JavaScript.
Quick solution:
// add onsubmit event to the form, specify submit handling function and form fields
<form id="my-form" onsubmit="submitHandlingFunction(event)">
Form field: <input type="text">
<input type="submit" />
</form>
Practical example
In this example, we will submit my-form
. To do so, we use onsubmit
event and submitForm()
event handling function.
// ONLINE-RUNNER:browser;
<!doctype html>
<html>
<body>
<form id="my-form" onsubmit="submitForm(event)">
Username: <input type="text" /><br />
Password: <input type="password" /><br />
<input type="submit" value="Submit" />
</form>
<script>
var myForm = document.querySelector('#my-form');
function submitForm(event) {
event.preventDefault();
console.log('onsubmit event occurred...');
}
</script>
</body>
</html>