Your Search Bar For Shrewd Tips

How Do I Clear Input Field In Jquery?


To clear an input field in jQuery, select the field and use the .val('') method. For example, $('#name').val(''); empties the input with the ID name. This simple approach works for most text-based fields and is useful for clearing a form after submission, resetting a search box, or responding to a button click.

The right solution depends on what kind of field you want to clear. Text inputs, textareas, dropdowns, checkboxes, radio buttons, and file inputs do not all behave the same way. This guide explains how to clear input fields in jQuery, shows practical examples, and covers common mistakes so you can choose the right method for your form.

The Basic Way to Clear an Input Field in jQuery

Use jQuery’s .val() method with an empty string to remove the current value from a text input. The method both reads and sets the value of a form element. When you pass an empty string, it sets the value to nothing.

$('#email').val('');

This line selects the element with the ID email and clears its current text. You can use the same pattern with other selectors, such as a class or an input type:

  • $('.search-box').val(''); clears every matching element with the class search-box.
  • $('input[type="text"]').val(''); clears all text inputs on the page.
  • $('#contact-form input').val(''); clears input elements inside the form with the ID contact-form.

Be specific with selectors. A broad selector can clear fields you meant to keep, including hidden values or inputs in unrelated forms. When possible, target a single ID, a clear class, or the inputs inside one particular form.

Clear an Input When a Button Is Clicked

A common use case is clearing a field when someone clicks a button. Attach a click handler to the button, then call .val('') on the input. For example, an input with the ID search can be cleared with a button called clear-search.

$('#clear-search').on('click', function () { $('#search').val(''); });

Using .on('click', ...) is the standard jQuery approach for attaching a click event. The function runs when the user activates the button, and the input is emptied at that time. If you want the cursor to return to the field after clearing it, focus the input too:

$('#clear-search').on('click', function () { $('#search').val('').focus(); });

For an inline search-clear button, you may also want to hide the button when the search box is empty. Check the current value with .val() and update the button’s visibility as the user types. Keep the button accessible: use a real button element and give it a descriptive label, such as “Clear search.”

Clear All Fields in a Form

To clear several text fields at once, select the relevant elements within the form and set their values to an empty string. This example targets text inputs, email inputs, number inputs, and textareas in one form:

$('#contact-form').find('input[type="text"], input[type="email"], input[type="number"], textarea').val('');

The .find() method searches within the selected form, which helps avoid affecting fields elsewhere on the page. Adjust the selector to match the fields in your form. For example, include input[type="tel"] if you want to clear telephone fields as well.

You can also clear all input elements in a form with a shorter selector:

$('#contact-form').find(':input').val('');

However, :input includes more than ordinary text boxes. It can select buttons, checkboxes, radio buttons, select menus, and other form controls. Calling .val('') on every control may not produce the result you expect. Use a focused selector when the form contains several control types, or clear each type with the appropriate method.

Clear a Textarea or Dropdown

Textareas use .val('') in the same way as text inputs. For instance, $('#message').val(''); clears the text inside a textarea with the ID message. You do not need a separate jQuery method just because the field can contain multiple lines.

Dropdowns require a little more care. To clear a select menu, set its value to the value of an empty option, if the menu has one:

$('#category').val('');

For this to select a blank option, the HTML should include an option whose value is empty. If the dropdown does not contain a matching option, the result may be no selected option rather than a visible blank choice. If you want to return the dropdown to its first option, use selectedIndex:

$('#category').prop('selectedIndex', 0);

Choose the behavior that fits your form. Selecting a blank prompt such as “Choose a category” is different from selecting the first actual category. Make sure the dropdown’s initial option and its validation rules work together.

Clear Checkboxes and Radio Buttons

Checkboxes and radio buttons have a checked state as well as a value. The .val('') method changes a control’s value; it does not uncheck the control. To clear a checkbox, set its checked property to false:

$('#terms').prop('checked', false);

To uncheck every checkbox in a form, use a selector scoped to that form:

$('#contact-form input[type="checkbox"]').prop('checked', false);

Radio buttons work similarly. To clear a group, remove the checked state from its radio inputs:

$('#contact-form input[type="radio"]').prop('checked', false);

In many forms, a radio group is designed to require one choice, so leaving every option unchecked may not be appropriate. Consider whether your form should allow users to clear their selection and whether its validation logic supports that state.

Clear a File Input Safely

File inputs have browser security restrictions. A script cannot fill a file input with an arbitrary path or select a file on the user’s behalf. If a user has chosen a file and you need to remove that selection, setting the value to an empty string is the usual approach:

$('#attachment').val('');

This clears the selected file in modern browsers. Do not try to set a file input to a non-empty value; browsers block that behavior to protect users. If a file field cannot be cleared reliably in a particular setup, replacing it with a fresh file input is another option, though it should be done carefully so labels and event handlers continue to work.

Clear Fields After Form Submission

You may want to clear a form after it has been submitted successfully. If the form is handled with AJAX, clear the fields in the success callback rather than immediately when the user clicks Submit. That way, the entered information remains available if the request fails.

$.ajax({

  url: '/submit',

  method: 'POST',

  data: $('#contact-form').serialize(),

  success: function () { $('#contact-form')[0].reset(); }

});

In this example, the form is reset only after a successful response. The native form method reset() restores each field to its original default value; it does not always make every field blank. If your defaults are blank, that is a convenient way to clear the form. If you need particular controls to be empty regardless of their defaults, set them explicitly with jQuery.

For a normal form submission that navigates to another page, the browser typically reloads or leaves the page, so clearing the old form may not be necessary. If submission is canceled with preventDefault(), decide whether to clear only after your own validation and submission logic succeeds.

Clear a Field When the Page Loads

To empty a field as soon as the page is ready, run the jQuery statement inside a document-ready handler:

$(function () { $('#search').val(''); });

This is useful when a field may contain a value restored by the browser or populated by another script. Use it only when clearing that value is intentional. Automatically removing information users expect to keep can be frustrating, especially when they return to a form after a failed submission.

Common Mistakes to Avoid

  • Using .text('') on an input. Inputs and textareas store their content as a value, so use .val('') instead.
  • Using .val('') to uncheck a checkbox. Set .prop('checked', false) to change its checked state.
  • Clearing every input on the page by accident. Scope selectors to the form or field group you intend to reset.
  • Assuming form reset always means blank. The browser restores default values, which may be prefilled.
  • Clearing fields before checking for errors. Validate first so users do not lose information when the form is invalid.
  • Expecting an empty dropdown value without a matching option. Add a blank option or explicitly select the intended item.

jQuery and Modern JavaScript Alternatives

These examples assume jQuery is loaded on the page. If you see an error such as $ is not defined, check that the jQuery library is included before your script runs. If your project does not use jQuery, modern JavaScript can clear a field directly with document.querySelector('#search').value = '';.

For a jQuery project, .val('') remains a clear and readable way to empty standard text fields. Use jQuery’s .prop() for properties such as whether a checkbox is checked, and use a form’s native reset() method when restoring default values is the desired behavior.

Conclusion

The simplest answer to “How do I clear an input field in jQuery?” is to select the field and call .val(''). Use it for text inputs and textareas, target only the fields you intend to clear, and choose a matching blank option when working with a dropdown. For checkboxes and radio buttons, change the checked property with .prop('checked', false). For a form reset, remember that the browser restores default values rather than guaranteeing blank fields.

By matching the jQuery method to the type of form control, you can clear one input, reset a group of fields, or clean up a form after a successful submission without surprising users or losing data unnecessarily.


Disclaimer: Articles are written by Humans, AI or Both. Verify Important information.

Shrewdnia

Shrewdnia

Shrewdnia is a destination for curious minds seeking clarity, knowledge, and informed perspectives. Through insightful articles and practical guides our passionate team explores a wide range of topics designed to help readers understand the world around them, make smarter decisions, and stay informed in an ever-changing landscape.


💡 Every question sparks discovery, and every perspective enriches the conversation. Share your thoughts and insights in the comments 👇

Back to blog

Leave a comment

JOIN THE SHREWDNIA COMMUNITY FORUM

What do you think?

Have an opinion, experience, or question about this topic? Join the Shrewdnia Forum and share your thoughts with other readers.

Join the Forum →