ingh.am
ingh.am

Reputation: 26742

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

Upvotes: 61

Views: 215210

Answers (9)

this is might be a possible solution

void 0 != document.getElementById("ad") &&       (document.getElementById("ad").onclick =function(){
 var a = $("#client_id").val();
 var b = $("#contact").val();
 var c = $("#message").val();
 var Qdata = { client_id: a, contact:b, message:c }
 var respo='';
     $("#message").html('');

return $.ajax({

    url: applicationPath ,
    type: "POST",
    data: Qdata,
    success: function(e) {
         $("#mcg").html("msg send successfully");

    }

})

});

Upvotes: -2

Jose
Jose

Reputation: 1897

This worked for me:

$("#searchField").focus(function()
{ 
    this.value = ''; 
});

Upvotes: 0

salar
salar

Reputation: 41

Try using this:

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

Upvotes: 1

AnOldMan
AnOldMan

Reputation: 11

I know this is an old post, but this may help clarify:

$('#searchField')
    .val('')// [property value] e.g. what is visible / will be submitted
    .attr('value', '');// [attribute value] e.g. <input value="preset" ...

Changing [attribute value] has no effect if there is a [property value]. (user || js altered input)

Upvotes: 1

user2985029
user2985029

Reputation:

With jQuery, I've found that sometimes using val to clear the value of a textbox has no effect, in those situations I've found that using attr does the job

$('#searchField').attr("value", "");

Upvotes: 10

lukad
lukad

Reputation: 17833

Use it like this:

$("#searchField").focus(function() {
    $(this).val("");
});

It has to work. Otherwise it probably never gets focused.

Upvotes: 3

pimvdb
pimvdb

Reputation: 154818

First, select the element. You can usually use the ID like this:

$("#searchField"); // select element by using "#someid"

Then, to set the value, use .val("something") as in:

$("#searchField").val("something"); // set the value

Note that you should only run this code when the element is available. The usual way to do this is:

$(document).ready(function() { // execute when everything is loaded
    $("#searchField").val("something"); // set the value
});

Upvotes: 0

Senad Meškin
Senad Meškin

Reputation: 13756

To set value

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

to retrieve value

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

Upvotes: 1

David Laberge
David Laberge

Reputation: 16031

In Javascript :

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

In jQuery :

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

That should do it

Upvotes: 128

Related Questions