Rita
Rita

Reputation: 735

How to clear the Text on Clear button using JQuery

I have a clear button that needs to clear the TextBox value(that is entered) on this button click using JQuery function.

Upvotes: 2

Views: 16893

Answers (3)

Hogan
Hogan

Reputation: 70523

Lets say you have a "thingtoclear" and a "thingtoclick" then you might want something like this:

    $(document).ready( function() {
       $('#thingtoclick').click( function () {
             $('#thingtoclear').val(""); 
       });
     });

Upvotes: 2

Jace Rhea
Jace Rhea

Reputation: 5018

You just need to attach a click event to the button that will set the value of the input element to empty.

$("#clearButton").click(function() {
        $("#textbox1").val("");
});

Of course to give you exact selectors we need to see the actual html. The above jquery call would work for this html.

<button type="button" id="clearButton">Clear</button>
<input id="textbox1" type="text" value="" />

Upvotes: 4

griegs
griegs

Reputation: 22760

$('#' + fieldName).val("");

or you can use a class name like;

$('.' + fieldName).val("");

Class names are better sometimes when id's are changed in the environment.

Upvotes: 2

Related Questions