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:
xxxxxxxxxx
1
// add onsubmit event to the form, specify submit handling function and form fields
2
<form id="my-form" onsubmit="submitHandlingFunction(event)">
3
Form field: <input type="text">
4
<input type="submit" />
5
</form>
In this example, we will submit my-form
. To do so, we use onsubmit
event and submitForm()
event handling function.
xxxxxxxxxx
1
2
<html>
3
<body>
4
<form id="my-form" onsubmit="submitForm(event)">
5
Username: <input type="text" /><br />
6
Password: <input type="password" /><br />
7
<input type="submit" value="Submit" />
8
</form>
9
<script>
10
11
var myForm = document.querySelector('#my-form');
12
13
function submitForm(event) {
14
event.preventDefault();
15
console.log('onsubmit event occurred...');
16
}
17
18
</script>
19
</body>
20
</html>