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
None of the above works on a simple case when the page includes a call to web user control that involves IHttpHandler request processing (captcha). After sending the requsrt (for image processing) the code below does not clear the fields on the form (before sending the HttpHandler request ) everythings works correctly.
<input type="reset" value="ClearAllFields" onclick="ClearContact()" />
<script type="text/javascript">
function ClearContact() {
("form :text").val("");
}
</script>
Any reason this shouldn't be used?
$("#form").trigger('reset');
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:
$(':input', this)
.not(':button, :submit, :reset, :hidden')
.each(function(i,e) {
$(e).val($(e).attr('value') || '')
.prop('checked', false)
.prop('selected', false)
})
$('option[selected]', this).prop('selected', true)
$('input[checked]', this).prop('checked', true)
$('textarea', this).each(function(i,e) { $(e).val($(e).html()) })
Please let me know if I'm missing anything or anything can be improved.
I use this :
$(".reset").click(function() {
$('input[type=text]').each(function(){
$(this).val('');
});
});
And here is my button:
<a href="#" class="reset">
<i class="fa fa-close"></i>
Reset
</a>
$(".reset").click(function() {
$(this).closest('form').find("input[type=text], textarea").val("");
});
If i want to clear all the fields except accountType..Use the following
$q(':input','#myform').not('#accountType').val('').removeAttr('checked').removeAttr('selected');