user2625152
user2625152

Reputation: 517

How to erase the text input that the user typed using JavaScript

<div class = "search ui-widget">
    <label for = "keyword"></label>
    <input type="text" id="keyword" onkeypress="searchKeyPress(event)" placeholder="Search here" required>
    <input type="button" id="btnSearch" onclick="loadDeals('search', keyword.value,'')"  />
</div>


$('.search input#keyword').Value('');

Basically what I want is to remove the user's input in the text box after the user clicks another menu tab. I tried $('.search input#keyword').Value(''); and $('.search input#keyword').css("value", ''); but it didn't work.

Upvotes: 0

Views: 180

Answers (4)

Afzaal Ahmad Zeeshan
Afzaal Ahmad Zeeshan

Reputation: 15860

Use this

$("the_class_or_id").val("");

Link for this: jQuery Documentation

This is introduced in jQuery API. You can use .value in JavaScript, but in jQuery its val(). It gets the value of the object and to clear the value, just add quotes!

JavaScript code would be:

document.getElementById("id_name").value = "";

Upvotes: 0

bpmason1
bpmason1

Reputation: 1940

Since HTML id attributes are supposed to be unique I would recommend not using the '#keyword' id in your jquery selector. The solution does work if there's only one text field, but it isn't scalable to multiple text fields. Instead, I would make 'keyword' a class for the input element and use the selector:

$('.search input.keyword').val('');

This is very similar to the solution Sergio gave except it allows you to control, via the 'keyword' class, which input elements have their values cleared.

Upvotes: 0

Sergio
Sergio

Reputation: 28837

.val() is the right name of the jQUery method, not Value().

You can use jQuery like this:

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

Or you can use plain javascript like this:

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

If there are more input fields beside the ones you posted and you want to clear all inputs you can use:

$('.search input').val('');

Upvotes: 3

Joon
Joon

Reputation: 9884

Here's a pure javascript solution:

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

Upvotes: 1

Related Questions