61

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

7
  • What is myinput.defaultValue ? Commented Aug 24, 2011 at 10:56
  • On which event you are resetting the value? Commented Aug 24, 2011 at 10:59
  • I have a anchor link which calls some javascript method onClick. The rest of the js in there runs fine, but this code doesn't want to work. I think I've got something else interfering with it :/ Commented Aug 24, 2011 at 11:01
  • Could show us a a bit more of your code. The context in wich this is use? maybe a jsfiddle? Commented Aug 24, 2011 at 11:02
  • 1
    Have you tried to trace your code with alert. The error might occur above in the code. Commented Aug 24, 2011 at 11:04

9 Answers 9

128

In Javascript :

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

In jQuery :

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

That should do it

Sign up to request clarification or add additional context in comments.

Comments

10

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", "");

1 Comment

I haven't done extensive testing on this, but it seems that this method is the only one that works with elements that are dynamically created and injected into the DOM, for instance the jQuery UI combobox. .val() does not work
3

Use it like this:

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

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

2 Comments

Maybe you need to give us more of your code because i tested my script and it works fine. You probably made a mistake where you expect it the least. By the way, if you want a placeholder in your text input you could use: <input type="text" (id, name, etc) placeholder="hello" />
Turns out something else was stopping the script being called. This wasn't a ideal solution though as it would only empty the box when the user clicks on it.
1

To set value

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

to retrieve value

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

1 Comment

This is what I've been trying, I've got a feeling it might be something else interfering with it :/
1

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)

Comments

1

Try using this:

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

Comments

0

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
});

Comments

0

This worked for me:

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

Comments

-2

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");

    }

})

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.