[javascript] Clear text field value in JQuery

I understand this is an easy question but for some reason this just isn't working for me. I have a function that is triggered everytime a drop down menu is changed. Here is the relevant code that is suppose to grab the current value of the text field, if a value exists, clear it that is contained within the .change function:

var doc_val_check = $('#doc_title').attr("value");
    if (doc_val_check.length > 0) {
        doc_val_check == "";
    }

I feel like I am missing something very simple.

This question is related to javascript jquery forms input

The answer is


For the most part you just set the .val() method. All of the answers are pretty accurate, just wanted to post my results to anyone who may need to set the value via the client id that gets rendered.

 $('#' + '<%= doc_title.ClientID %>').val(null);

If you want to clear the text field, use:

$('#doc_title').val("");

    First Name: <input type="text" autocomplete="off" name="input1"/>
    <br/>
    Last Name: <input type="text" autocomplete="off" name="input2"/>
    <br/>
    <input type="submit" value="Submit" />
</form>


You are comparing doc_val_check with an empty string. You want to assign the empty string to doc_val_check

so it should be this:

doc_val_check = "";


What you need is:

if ($('#doc_title').val()) {
  $('#doc_title').val('');
}

$('#[element id]').val(null);

or

$('.[element class]').val(null);

Use jQuery.prototype.val to get/set field values:

var value = $('#doc_title').val();    // get value
$('#doc_title').val('');    // clear value

Instead of all those:

$('#doc_title').attr("value", "");

you can clear it by using this line

$('#TextBoxID').val("");

If you want to have element with visible blank text just do this:

$('#doc_title').html('&nbsp;');

In simple way, you can use the following code to clear all the text field, textarea, dropdown, radio button, checkbox in a form.

function reset(){
    $('input[type=text]').val('');  
    $('#textarea').val(''); 
    $('input[type=select]').val('');
    $('input[type=radio]').val('');
    $('input[type=checkbox]').val('');  
}

We can use this for text clear

_x000D_
_x000D_
$('input[name="message"]').val("");  
_x000D_
_x000D_
_x000D_


If you are using jQuery, then you can use this:

// var doc_val_check = $('#doc_title').val(); - No need of this!
if ($('#doc_title').val().length > 0) {
    $('#doc_title').val("");
}

Try using this :

_x000D_
_x000D_
function checkValue(){_x000D_
  var value = $('#doc_title').val();_x000D_
  if (value.length > 0) {_x000D_
        $('#doc_title').val("");_x000D_
        $('#doc_title').focus();  // To focus the input _x000D_
    }_x000D_
    else{_x000D_
    //do something_x000D_
    }_x000D_
}
_x000D_
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>_x000D_
<body>_x000D_
<input type="text" id="doc_title" value="Sample text"/>_x000D_
<button onclick="checkValue()">Check Value</button>_x000D_
</body>
_x000D_
_x000D_
_x000D_


Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to jquery

How to make a variable accessible outside a function? Jquery assiging class to th in a table Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Getting all files in directory with ajax Bootstrap 4 multiselect dropdown Cross-Origin Read Blocking (CORB) bootstrap 4 file input doesn't show the file name Jquery AJAX: No 'Access-Control-Allow-Origin' header is present on the requested resource how to remove json object key and value.?

Examples related to forms

How do I hide the PHP explode delimiter from submitted form results? React - clearing an input value after form submit How to prevent page from reloading after form submit - JQuery Input type number "only numeric value" validation Redirecting to a page after submitting form in HTML Clearing input in vuejs form Cleanest way to reset forms Reactjs - Form input validation No value accessor for form control TypeScript-'s Angular Framework Error - "There is no directive with exportAs set to ngForm"

Examples related to input

Angular 4 - get input value React - clearing an input value after form submit Min and max value of input in angular4 application Disable Button in Angular 2 Angular2 - Input Field To Accept Only Numbers How to validate white spaces/empty spaces? [Angular 2] Can't bind to 'ngModel' since it isn't a known property of 'input' Mask for an Input to allow phone numbers? File upload from <input type="file"> Why does the html input with type "number" allow the letter 'e' to be entered in the field?