How to make my textfield empty after button click in JavaScript?

For this, you can use onclick=”yourFunctionName()”, and −



 Live Demo

<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initialscale=1.0">
<link rel="stylesheet" href="//">
<script src=""></script>
<script src=""></script>
<link rel="stylesheet" href="">
<input type-="text" id="txtBox" placeholder="Enter the value...."/>
<button type="button" id="clear" onclick="clearTheTextField()">ClearInputText</button>
   function clearTheTextField(){
      console.log("The text box
      document.getElementById('txtBox').value = '';

To run the above program, just save the file name anyName.html(index.html) and 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 click the button ClearInputText.

After entering the value, clicking the button.

This will produce the following output −