JavaScript Create Submit button for form submission and another button to clear input

JavascriptWeb DevelopmentObject Oriented Programming

Use input type=”submit” to submit the form and another input type=”button” to clear the input on click as in the below code −


 Live Demo

<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initialscale=
<link rel="stylesheet" href="//">
<script src=""></script>
<script src=""></script>
<link rel="stylesheet" href="">
<input type="text" id="txtName" />
<button type="submit">Submit</button>
<button type=" button" value="delete"
   function deleteSomething(secondValue) {
      alert("I am going to clear text box data only...");
      document.getElementById("txtName").value = "";

To run the above program, save the file name “anyName.html(index.html)” and right click on the file. Select the option “Open with Live Server” in VS Code editor.


This will produce the following output −

Now, I am going to enter some value into the text box −

Now, I am going to click the button Delete. On clicking, you will get an alert message −

After clicking the OK button, you will get the following output −

Published on 01-Sep-2020 14:14:08