Clear text field value in JQuery
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('');
}
Yuschick
Updated on January 06, 2020Comments
-
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 almost 12 yearshe can use
.val()
function. -
Praveen Kumar Purushothaman almost 12 yearsThe
.html()
doesn't apply here. -
Anthony Grist almost 12 yearsCan 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 almost 12 yearsThis worked like a charm. Thank you. Once I'm allowed to, I'll select this as the answer.
-
xdazz almost 12 years@PraveenKumar Yep, that's true.
-
Praveen Kumar Purushothaman almost 12 years@Yuschick can be made still simpler.
-
Anthony Grist almost 12 yearsCalling
doc_val_check = ""
won't update thevalue
property of the element; you'll just end up with a variable that's an empty string that's completely separate from your element'svalue
property. -
Doug Neiner almost 12 yearsThe ones that use
.val()
are better answers. This would be non-typical jQuery code to useattr
to change the value. -
Tooraj Jam almost 12 yearsSummery of all answers after while!
-
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 almost 12 years@Tooraj thanks bro, I like to get suggestion from all and try to update myself.
-
Hrushi almost 8 yearsuse autocomplete="off"
-
Asad Ali over 7 yearsIt won't work for clear text - we have to use jquery for clear text
-
MD Shahrouq about 6 yearsSince I was echoing the php session value , the $('#doc_title').attr("value", ""); didn;t worked with me.