
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- 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() in 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 −
Advertisements