Clear text field value in JQuery

JavascriptJqueryFormsInput

Javascript Problem Overview


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.

Javascript Solutions


Solution 1 - Javascript

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 - Javascript

If you want to clear the text field, use:

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

Solution 3 - Javascript

Instead of all those:

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

Solution 4 - Javascript

What you need is:

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

Solution 5 - Javascript

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('');	
}

Solution 6 - Javascript

you can clear it by using this line

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

Solution 7 - Javascript

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("");
}

Solution 8 - Javascript

We can use this for text clear

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

Solution 9 - Javascript

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);

Solution 10 - Javascript

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 = "";

Solution 11 - Javascript

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

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

Solution 12 - Javascript

> >

>
> First Name: >
> Last Name: >
> >
> >

Solution 13 - Javascript

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

or

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

Solution 14 - Javascript

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

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

Solution 15 - Javascript

Try using this :

function checkValue(){
  var value = $('#doc_title').val();
  if (value.length > 0) {
        $('#doc_title').val("");
        $('#doc_title').focus();  // To focus the input 
    }
    else{
    //do something
    }
}

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body>
<input type="text" id="doc_title" value="Sample text"/>
<button onclick="checkValue()">Check Value</button>
</body>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionYuschickView Question on Stackoverflow
Solution 1 - JavascriptthecodeparadoxView Answer on Stackoverflow
Solution 2 - JavascriptthebiffboffView Answer on Stackoverflow
Solution 3 - JavascriptTooraj JamView Answer on Stackoverflow
Solution 4 - JavascriptxdazzView Answer on Stackoverflow
Solution 5 - JavascriptSankarView Answer on Stackoverflow
Solution 6 - JavascriptKamran KhanView Answer on Stackoverflow
Solution 7 - JavascriptPraveen Kumar PurushothamanView Answer on Stackoverflow
Solution 8 - JavascriptAsad AliView Answer on Stackoverflow
Solution 9 - Javascriptcking24343View Answer on Stackoverflow
Solution 10 - JavascriptSherCoderView Answer on Stackoverflow
Solution 11 - JavascriptAxelView Answer on Stackoverflow
Solution 12 - JavascriptHrushiView Answer on Stackoverflow
Solution 13 - JavascriptricarelaView Answer on Stackoverflow
Solution 14 - JavascriptHamed NikzadView Answer on Stackoverflow
Solution 15 - JavascripttrickymindView Answer on Stackoverflow