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
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(' ');
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
$('input[name="message"]').val("");
_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 :
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_
Source: Stackoverflow.com