Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 912
Yuschick Avatar asked Jun 08 '12 15:06

Yuschick


People also ask

How do you clear a text field in JavaScript?

To clear an input field after submitting:Add a click event listener to a button. When the button is clicked, set the input field's value to an empty string. Setting the field's value to an empty string resets the input.

How do you clear a textbox in HTML?

To clear all the input in an HTML form, use the <input> tag with the type attribute as reset.

How do you clear the input history in HTML?

That solution helped me: if you are using google Chrome, try this: Put the mouse pointer on the entry that you want to delete, press Delete . If the entry is not removed, press Shift + Delete .


1 Answers

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

like image 93
thecodeparadox Avatar answered Oct 15 '22 00:10

thecodeparadox