How to clear an input field in JavaScript? 4 Effective Ways

Are you looking for a solution to clear an input field in JavaScript? Look no further!

In this article, we will show you how to clear an input field using various methods such as the value property, setAttribute method, and reset method.

Aside from that, we also provide code examples and explanations for each method.

How to clear an input field in JavaScript?

There are several ways how to clear an input field in JavaScript. The following are a few examples:

Using the value property

You can clear an input field in JavaScript by setting the value of the input field to an empty string.

Here’s an example:

document.getElementById("inputField").value = '';

This code selects the input element with the id “inputField” and sets its value to an empty string, effectively clearing the input field.

The value property represents the current value of the input field.

Here’s the complete code:

<!DOCTYPE html>
<html>
<head>
  <style>
    body {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      height: 100vh;
      margin: 0;
    }
  </style>
</head>
<body>

<h2 style="color:blue">Welcome to Itsourcecode</h2>
<h2 style="color:red">How to clear an input field in JavaScript?</h2>
<p>The input field will be cleared, when you clicked button</p>

<div style="display: flex;">
  <button onclick="clearInput()">Clear input field</button>
  <input type="text" id="inputField" value="Welcome to Itsourcecode!">
</div>

<script>
function clearInput() {
  document.getElementById('inputField').value = '';
}
</script>

</body>
</html>

Output:

Using the setAttribute method

document.getElementById('inputField').setAttribute('value', '');

This solution uses the setAttribute method to set the value attribute of the input field to an empty string, which clears the input field.

The setAttribute method is used to set the value of an attribute on an element.

Here’s the complete example:

<!DOCTYPE html>
<html>
<head>
  <style>
    body {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      height: 100vh;
      margin: 0;
    }
  </style>
</head>
<body>

<h2 style="color:blue">Welcome to Itsourcecode</h2>
<h2 style="color:red">How to clear an input field in JavaScript?</h2>
<p>The input field will be cleared, when you clicked button</p>

<div style="display: flex;">
  <button onclick="clearInput()">Clear input field</button>
  <input type="text" id="inputField" value="Welcome to Itsourcecode!">
</div>

<script>
function clearInput() {
  document.getElementById('inputField').setAttribute('value', '');
}
</script>
</body>
</html>

Output:

The script in the code you provided uses the setAttribute method to clear the input field only once.

When the “Clear input field” button is clicked for the first time, the clearInput function is called, which sets the value attribute of the input element to an empty string using the setAttribute method. This clears the input field.

However, if you enter a new value into the input field and click the “Clear input field” button again, the input field will not be cleared.

This is because setting the value attribute using the setAttribute method only changes the default value of the input field, not its current value.

To clear the input field multiple times, you can use the value property instead of the setAttribute method.

Using the reset method

document.getElementById('Form').reset();

The reset method is used in the clearInput function to reset the form with id=”Form.”

When the “Clear form” button is clicked, the clearInput function is called, which resets all form controls to their initial values, including clearing the input field.

<!DOCTYPE html>
<html>
<head>
  <style>
    body {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      height: 100vh;
      margin: 0;
    }
  </style>
</head>
<body>

<h2 style="color:blue">Welcome to Itsourcecode</h2>
<h2 style="color:red">How to clear an input field in JavaScript?</h2>
<p>The input field will be cleared, when you clicked button</p>

<form id="Form">
  Employee ID: <input type="text">
  <input type="button" onclick="clearInput()" value="Clear form"><br><br>
</form>

<script>
function clearInput() {
  document.getElementById("Form").reset();
}
</script>

</body>
</html>

Output:

Using onfocus event

You can also use the onfocus event to clear an input field in JavaScript.

The onfocus event is triggered when an element receives focus, such as when a user clicks on an input field or navigates to it using the keyboard.

Here’s an example of how you can use the onfocus event to clear an input field:

<!DOCTYPE html>
<html>
<head>
  <style>
    body {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      height: 100vh;
      margin: 0;
    }
  </style>
</head>
<body>

<h2 style="color:blue">Welcome to Itsourcecode</h2>
<h2 style="color:red">How to clear an input field in JavaScript?</h2>
<p>The input field will be cleared, when you clicked button</p>

<input type="text" id="inputField" value="Welcome to Itsourcecode!" onfocus="clearInput()"><br><br>


<script>
function clearInput() {
  document.getElementById('inputField').value = '';
}
</script>

</body>
</html>

As you can see, the onfocus event is added to the input field using the onfocus attribute.

When the input field receives focus, the clearInput function is called, which sets the value property of the input field to an empty string, clearing the input field.

Output:

Conclusion

In conclusion, we have discussed several effective ways to clear an input field in JavaScript.

These methods include using the value property, setAttribute method, reset method, and onfocus event.

Each of these methods provides a different approach to clearing an input field, and you can choose the one that best fits your needs.

By using these methods, you can easily and effectively clear inputs in your code, improving the user experience and functionality of your web applications.

We are hoping that this article provides you with enough information that helps you understand the clear input field JavaScript.

You can also check out the following article:

Thank you for reading itsourcecoders 😊.

Frequently Asked Questions

Is JavaScript still worth learning in 2026?
Yes. JavaScript runs on 98% of websites for the front-end, dominates the back-end via Node.js, powers mobile apps through React Native, builds desktop tools through Electron, and is the scripting layer for most AI tooling (LangChain.js, OpenAI SDK, Vercel AI). Whether you target web, mobile, AI, or full-stack capstones, JavaScript is the broadest single language you can learn.
What is the difference between var, let, and const?
var is function-scoped, hoisted to the top of its scope, and can be redeclared, which leads to bugs in modern code. let is block-scoped (only visible inside the nearest {}) and can be reassigned. const is block-scoped and cannot be reassigned, although object contents can still mutate. Default to const for everything, switch to let only when you actually need to reassign, and avoid var in any code written after 2017.
Which JavaScript version should I target in 2026?
Target ES2020 (ES11) as the safe baseline because every modern browser and Node.js 14+ supports it fully. ES2022 adds useful features like top-level await, private class fields with the # prefix, and the .at() array method. If you are writing for older browsers (IE11 or older Android WebViews), transpile down with Babel or use a build tool like Vite, esbuild, or webpack.
What is the best free editor for JavaScript?
Visual Studio Code is the industry standard, free, with built-in IntelliSense, debugger, terminal, Git, and a huge extension marketplace (ESLint, Prettier, GitHub Copilot, Tailwind). Install the JavaScript and TypeScript Nightly extension for the latest language features. JetBrains WebStorm is more powerful and free for students with a verified .edu email. For quick scratchpad work, the Chrome DevTools Sources panel includes a workspace and breakpoint debugger.
How do I run JavaScript locally vs in the browser?
In the browser: open DevTools with F12 (or right-click then Inspect), go to the Console tab, type or paste your code, press Enter. For HTML pages, add a script tag pointing to your .js file. Locally with Node.js: download Node from nodejs.org (LTS version), then run node script.js in your terminal from the file folder. Use the same Node setup for backend capstones, API integrations, and scripts that do not need a browser.
What can I build with JavaScript for my BSIT capstone?
Common BSIT capstones in JavaScript: full-stack web apps using React or Vue on the front-end with Node.js and Express on the back-end (MongoDB or MySQL for the database), real-time chat or notification systems using Socket.io, single-page dashboards with Chart.js or D3.js, cross-platform mobile apps with React Native, AI-powered chatbots using OpenAI SDK and LangChain.js, and Chrome extensions for productivity tools. Add Tailwind CSS for the UI and Vercel or Netlify for free deployment.
Caren Bautista

Technical Writer at PIES IT Solution

Responsible for crafting clear, well-structured, and beginner-friendly content across the platform. Handles the writing, proofreading, and editorial review of tutorials, guides, and documentation to ensure every article is accurate, readable, and easy to follow.

Expertise: Technical Writing · Content Creation · Documentation · Editorial Writing · JavaScript · TypeScript · Python · Python Errors · HTTP Errors · MS Excel  · View all posts by Caren Bautista →

Leave a Comment