
- HTML Tutorial
- HTML - Home
- HTML - Overview
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Formatting
- HTML - Phrase Tags
- HTML - Meta Tags
- HTML - Comments
- HTML - Images
- HTML - Tables
- HTML - Lists
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML - Frames
- HTML - Iframes
- HTML - Blocks
- HTML - Backgrounds
- HTML - Colors
- HTML - Fonts
- HTML - Forms
- HTML - Embed Multimedia
- HTML - Marquees
- HTML - Header
- HTML - Style Sheet
- HTML - Javascript
- HTML - Layouts
- HTML References
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Entities
- HTML - Fonts Ref
- HTML - Events Ref
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
How to prevent buttons from submitting forms in HTML?
To prevent forms from being submitted, we can use the onsubmit property. We can also use the event.preventDefault() method −
- Prevent buttons from submitting forms using the onsubmit property
- Prevent buttons from submitting forms using event.preventDefault()
Prevent buttons from submitting forms using the onsubmit property
We can prevent form from submitting using the onsubmit property as shown below −
<form onsubmit="return false;">
We have returned false above in the <form> tag itself to prevent form from submitting.
Example
Let us see the complete example −
<!doctype html> <html lang="en"> <body> <h1>Details</h1> <form onsubmit="return false;"> <p>Select your preferred subject:</p> <div> <input type="radio" id="maths" name="subject" value="maths"> <label for="maths">Maths</label> <input type="radio" id="science" name="subject" value="science"> <label for="science">Science</label> <input type="radio" id="english" name="subject" value="english"> <label for="english">English</label> </div><br> <div> <button id="submit" type="submit">Submit</button> </div> </form> </body> </html>
Output

Select any Subject radio button above and click Submit. It won’t get submitted −

Prevent buttons from submitting forms using event.preventDefault()
We can prevent form from submitting using the event.preventDefault() method −
<form onsubmit="event.preventDefault();">
Example
Let us now see the complete example −
<!doctype html> <html lang="en"> <body> <h1>Details</h1> <form onsubmit="event.preventDefault();"> <p>Select your preferred subject:</p> <div> <input type="radio" id="maths" name="subject" value="maths"> <label for="maths">Maths</label> <input type="radio" id="science" name="subject" value="science"> <label for="science">Science</label> <input type="radio" id="english" name="subject" value="english"> <label for="english">English</label> </div><br> <div> <button id="submit" type="submit">Submit</button> </div> </form> </body> </html>
Output

Select any Subject radio button above and click Submit. It won’t get submitted −

- Related Articles
- How do we use radio buttons in HTML forms?
- How do we use checkbox buttons in HTML forms?
- How to prevent multiple inserts when submitting a form in PHP?
- How to prevent sticky hover effects for buttons on touch devices?
- How to use tables to structurize forms in HTML?
- How to allow multiple file uploads in HTML forms.
- How to clear all the input in HTML forms?
- How to use the submit button in HTML forms?
- Prevent iPhone from zooming in web-app with HTML \n
- How to specify that the element must be filled out before submitting the form in HTML?
- How to take user input using HTML forms?
- How to Prevent Systems from Phishing Attacks?
- How to Prevent Your Website from Hackers?
- How to prevent form from being submitted?
- How we can group data in HTML forms?

Advertisements