JavaScript input type=submit is not working?

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

To make it work, you can use onclick with input type = “submit”.


Following is the code −

<!DOCTYPE html>
<html lang="en">
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="//">
<script src=""></script>
<script src=""></script>
   <form action="#">
      <label for="firstName">First Name:</label>
      <input type="text" id="fName" name="firstName" placeholder="Enter your First Name" />
      <button type="submit" onclick="submitForm(event)">Submit</button>
<span id="result"></span>
   var firstName = null;
   function submitForm(evnt) {
      firstName = document.getElementById("fName").value;
      document.getElementById("result").innerHTML = "The First Name is=" + firstName;

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 some value into the text box and enter the Submit button.


This will produce the following output −

Now enter the Submit button. This will produce the following output −

Updated on 27-Oct-2020 09:53:50