I want to clear all input and textarea fields in a form. It works like the following when using an input button with the reset
class:
$(".reset").bind("click", function() {
$("input[type=text], textarea").val("");
});
This will clear all fields on the page, not just the ones from the form. How would my selector look like for just the form the actual reset button lives in?
If i want to clear all the fields except accountType..Use the following
I got easiest trick to reset form
or
With Javascript you can simply do it with this syntax
getElementById("your-form-id").reset();
you can also use jquery by calling the reset function this way
$('#your-form-id')[0].reset();
Remember not to forget
[0]
. You will get the following error ifJQuery also provides an event you can use
I tried and it works.
Note: Its important to notice that these methods only reset your form to their initial value set by the server on page load. This means if your input was set on the value 'set value' before you did a random change, the field will be reset to that same value after reset method is called.
Hope it helps
the code I see here and on related SO questions seems incomplete.
Resetting a form means setting the original values from the HTML, so I put this together for a little project I was doing based on the above code:
Please let me know if I'm missing anything or anything can be improved.
If someone is still reading this thread, here is the simplest solution using not jQuery, but plain JavaScript. If your input fields are inside a form, there is a simple JavaScript reset command:
More about it here: http://www.w3schools.com/jsref/met_form_reset.asp
Cheers!
Simple but works like a charm.