• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / jQuery / How to Clear an Input Field Using jQuery

How to Clear an Input Field Using jQuery

July 15, 2022 Leave a Comment

We can clear an input field using jQuery easily by making use of the jQuery val() method. We simply need to set the value of the input field to an empty string.

$(input).val("");

Let’s see a quick example of this below:


Let’s say we have the following HTML:

<div id="div1">
  <input type="text" id="userText" value="This is some default text"></input>
</div>

The input field in the example above will have the text “This is some default text.”. If we want to clear the input field in the example above, we would use the following jQuery code:

$("#userText").val("");

If you are using WordPress, don’t forget to change the $ to jQuery as below:

jQuery("#userText").val("");

Let’s do an interactive example of clearing an input field below.

Using jQuery to Clear an Input Field with a Click

Below we will have a simple input field. It will start prefilled with a random name to start, “Joe Smith”. We will then let the user clear the input.

Here is our simple HTML:

<div id="div1">
  <label for="fname">Full Name:</label>
  <input type="text" id="userText" value="Joe Smith">
  <div id="click-me">Clear input field above</div>
</div>

We will simply use the jQuery val() method to set the input field to an empty string. We will use the jQuery click() method when the user clicks the button below the input field to call our val() method.

Here is the jQuery code:

$("#click-me").click(function(){
  $("#userText").val("");
});

The final code and output for this example of how to clear an input field using jQuery is below:

Code Output:


Clear input field above

Full Code:

<div id="div1">
  <label for="fname">Full Name:</label>
  <input type="text" id="userText" value="Joe Smith">
  <div id="click-me">Clear input field above</div>
</div>

<script>

$("#click-me").click(function(){
  $("#userText").val("");
});

</script>

Note we can also do this using just plain JavaScript by targeting the value property.

Getting the Value From an Input Field Using jQuery

To get the value from an input field using jQuery, we can once again use the jQuery val() method.

Let’s say we have the following HTML:

<form>
  <label for="fname">Full Name:</label>
  <input type="text" id="fname" name="fname" value="Jane Smith">
  <button type="submit" value="Submit">Submit</button>
</form>

To get the value that the user has entered for “Full Name”, we would use the val() method.

var userInput = $("#fname").val();

Hopefully this article has been useful to help you understand how to clear an input field using jQuery.

Other Articles You'll Also Like:

  • 1.  How to Use jQuery to Change Button Text
  • 2.  Using jQuery to Add id to div
  • 3.  jQuery text – Set the text of an HTML Element
  • 4.  Using jQuery to Hide Element by Class
  • 5.  jQuery slideDown – How to Slide Down and Show an Element Using jQuery
  • 6.  Using jQuery to Get the Value of a Radio Button
  • 7.  Using jQuery to see if URL Contains Specific Text
  • 8.  jQuery clone – Making a Copy of an Element
  • 9.  Using jQuery to get Textarea Value
  • 10.  jQuery has – Check if an Element Has Other Elements

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy