Rajesh
Rajesh

Reputation: 123

Clear input when Submit is clicked

I am having an input field and a button. When I click the button, the input field should clear itself. My HTML is:

<input type="text" id="search_number" />
<button id="submit">Submit</button>

And the js is:

$('#submit').click(function(){
     $('#search_number').value = '';
});

It doesnt works.. Thanks..

Upvotes: 0

Views: 97

Answers (4)

asifsid88
asifsid88

Reputation: 4701

its

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

Upvotes: 1

Richard Dalton
Richard Dalton

Reputation: 35793

Use the val function to set the value. Passing in an empty string will clear the value:

$('#submit').click(function(){
     $('#search_number').val('');
});

Also, the hash (#) was missing from the selector for search_number

Working example - http://jsbin.com/ilutar/1/

Upvotes: 2

Grant Thomas
Grant Thomas

Reputation: 45083

You need to properly specify that the selector for search_number is an identifier (by prefixing with a # (hash)), and use val() instead (which will get the value if no value is specified, and set the value of one is):

$("#search_number").val("");

Upvotes: 3

Niels
Niels

Reputation: 49909

You have to do this:

$('#search_number').val("");
  1. Your selector did not have the # for an id
  2. You can't use .value with jQuery but .val()

Upvotes: 3

Related Questions