- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Display form values after clicking Submit button using event.preventdefault() - jQuery?
For this, use document.getElementById(“”) along with addEventListener().
Example
Following is the code −
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <script src="https://code.jquery.com/jquery-1.12.4.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <body> <form id="details"> FirstName: <label><input type="text" id="firstName" /> </label> <br> LastName: <label><input type="text" id="lastName" /> </label> <br> <input type="submit"> </form> </body> <script> const formDetails = document.getElementById("details"); formDetails.addEventListener("submit", async (ev) => { ev.preventDefault(); var fName = document.getElementById("firstName").value; var lName = document.getElementById("lastName").value; console.log("First Name=" + fName); console.log("Last Name=" + lName); }) </script> </html>
To run the above program, save the file name “anyName.html(index.html)”. Right click on the file and select the option “Open with Live Server” in VS Code editor.
This will produce the following output −
Now, enter value into the text box and click the Submit button.
After clicking the submit button, you will get the following output on console −
- Related Articles
- Can I submit form with multiple submit buttons using jQuery?
- How to enable and disable submit button using jQuery?
- How to submit a form using jQuery click event?
- How to add a Submit button after the end of the tableview using Swift?
- JavaScript Create Submit button for form submission and another button to clear input
- How to submit a form in Selenium webdriver if submit button can't be identified?
- jQuery :submit Selector
- How to change the background color after clicking the button in JavaScript?
- How to change the href value of <a> tag after clicking on button using JavaScript?
- How to calculate the sum of radio button values using jQuery?
- jQuery submit() with Examples
- How to submit an HTML form using JavaScript?
- Find the Sum of Radio button values jQuery?
- How to send row data when clicking the button using javascript?
- How to link a submit button to another webpage using HTML?

Advertisements