Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reset textbox value in javascript

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... :/

like image 572
ingh.am Avatar asked Aug 24 '11 10:08

ingh.am


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.


1 Answers

In Javascript :

document.getElementById('searchField').value = ''; 

In jQuery :

$('#searchField').val(''); 

That should do it

like image 144
David Laberge Avatar answered Oct 02 '22 14:10

David Laberge