EN
HTML - prevent button from submitting form
0 points
In this article, we would like to show you how to prevent the button from submitting the form in HTML.
Quick solution:
xxxxxxxxxx
1
<button type="button">Click me</button>
Note:
To prevent the button from submitting the form set
button
type to"button"
since its default type is"submit"
.
In this example, we set the button
element type to "button"
to prevent it from submitting the form.
xxxxxxxxxx
1
2
<html>
3
<body>
4
<form>
5
<label>
6
<span>input:</span>
7
<input type="text" name="field-1" />
8
</label>
9
<button type="button">Click me</button>
10
</form>
11
</body>
12
</html>
You can also prevent form from being submitted using preventDefault()
method.
xxxxxxxxxx
1
2
<html>
3
<body>
4
<form onsubmit="event.preventDefault()">
5
<input type="text" />
6
<input type="submit" value="Submit" />
7
</form>
8
</body>
9
</html>
or by returning false
on submit event:
xxxxxxxxxx
1
2
<html>
3
<body>
4
<form onsubmit="return false;">
5
<input type="text" />
6
<input type="submit" value="Submit" />
7
</form>
8
</body>
9
</html>