If I have a input textbox like this:
<input type="text" id="searchField" name="searchField" />
How can I set the value of the textfield using javascript or jQuery?
You would think this was simple but I've tried the following:
Using defaultvalue
var a = document.getElementById("searchField"); a.value = a.defaultValue;
Using jQuery
jQuery("#searchField").focus( function() { $(this).val(""); } );
Using js
document.getElementById("searchField").value = "";
None of them are doing it... :/
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.
To clear all the input in an HTML form, use the <input> tag with the type attribute as reset.
In Javascript :
document.getElementById('searchField').value = '';
In jQuery :
$('#searchField').val('');
That should do it
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With