Clear text field value in JQuery

403,452

Solution 1

doc_val_check == "";   // == is equality check operator

should be

doc_val_check = "";    // = is assign operator. you need to set empty value

                       // so you need =

You can write you full code like this:

var doc_val_check = $.trim( $('#doc_title').val() ); // take value of text 
                                                     // field using .val()
    if (doc_val_check.length) {
        doc_val_check = ""; // this will not update your text field
    }

To update you text field with a "" you need to try

$('#doc_title').attr('value', doc_val_check); 
// or 
$('doc_title').val(doc_val_check);

But I think you don't need above process.


In short, just one line

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

Note

.val() use to set/ get value in text field. With parameter it acts as setter and without parameter acts as getter.

Read more about .val()

Solution 2

If you want to clear the text field, use:

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

Solution 3

Instead of all those:

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

Solution 4

What you need is:

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

Solution 5

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('');  
}
Share:
403,452
Yuschick
Author by

Yuschick

Updated on January 06, 2020

Comments

  • Yuschick
    Yuschick over 4 years

    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.

  • Praveen Kumar Purushothaman
    Praveen Kumar Purushothaman almost 12 years
    he can use .val() function.
  • Praveen Kumar Purushothaman
    Praveen Kumar Purushothaman almost 12 years
    The .html() doesn't apply here.
  • Anthony Grist
    Anthony Grist almost 12 years
    Can and should. Value is a property, not an attribute; setting the attribute with .attr() isn't an absolute guarantee that you'll set the property.
  • Yuschick
    Yuschick almost 12 years
    This worked like a charm. Thank you. Once I'm allowed to, I'll select this as the answer.
  • xdazz
    xdazz almost 12 years
    @PraveenKumar Yep, that's true.
  • Praveen Kumar Purushothaman
    Praveen Kumar Purushothaman almost 12 years
    @Yuschick can be made still simpler.
  • Anthony Grist
    Anthony Grist almost 12 years
    Calling doc_val_check = "" won't update the value property of the element; you'll just end up with a variable that's an empty string that's completely separate from your element's value property.
  • Doug Neiner
    Doug Neiner almost 12 years
    The ones that use .val() are better answers. This would be non-typical jQuery code to use attr to change the value.
  • Tooraj Jam
    Tooraj Jam almost 12 years
    Summery of all answers after while!
  • thecodeparadox
    thecodeparadox almost 12 years
    @Tooraj In my answer I tried to explain all aspects, if you think its a summary then its that, but down vote is not expected
  • thecodeparadox
    thecodeparadox almost 12 years
    @Tooraj thanks bro, I like to get suggestion from all and try to update myself.
  • Hrushi
    Hrushi almost 8 years
    use autocomplete="off"
  • Asad Ali
    Asad Ali over 7 years
    It won't work for clear text - we have to use jquery for clear text
  • MD Shahrouq
    MD Shahrouq about 6 years
    Since I was echoing the php session value , the $('#doc_title').attr("value", ""); didn;t worked with me.