14

I want to reset the form after calling an ajax function. This is the code i gave in the jquery:

$("#frm_silder").reset();

Here frm_silder is the id of form. But when I'm using this code i got an eorror message like this.

$("#frm_silder").reset is not a function

In my html i give the id to form like this:

<form name="frm_silder" id="frm_silder" method="post">

So what is the problem in my code?

1
  • have you added or imported jquery library on the page Commented Jan 20, 2012 at 5:58

4 Answers 4

35

In jQuery

$('#frm_silder')[0].reset();

in Javascript

document.getElementById('frm_silder').reset()
Sign up to request clarification or add additional context in comments.

2 Comments

Thaks, woks for me too.
And for me too!
4

You need to reset each element individually. Jquery does not have a function reset() that works on a form. reset() is a Javascript function that works on form elements only. You can however define a new jquery function reset() that iterates through all form elements and calls the javascript reset() on each of them.

$(document).ready(function(){
    $('a').click(function(){
        $('#reset').reset();
    });
});

 // we define a function reset
jQuery.fn.reset = function () {
  $(this).each (function() { this.reset(); });
}

Demo

Alternatively, if you don't want to define a function, you can iterate through the form elements

$(document).ready(function() {
    $('a').click(function() {
        $('#reset').each(function() {
            this.reset();
        });
    });
});

Demo

Source

1 Comment

jQuery doesn't have a reset() method because JavaScript already has one. Calling reset() on a form element (see accepted answer above) will reset all form fields. Your method works in most cases, unless the form has a File input field. Your code will not work in all browsers in that case. The only sure way to clear a File input field is to use JavaScript's reset method.
0

I followed the solution given by @sameera. But it still throw me error.

I changed the reset to the following

$("form#frm_silder")[0].reset();

Then it worked fine.

Comments

0

You can use the following.

   @using (Html.BeginForm("MyAction", "MyController", new { area = "MyArea" }, FormMethod.Post, new { @class = "" }))
    {
        <div class="col-md-6">
            <div class="col-lg-3 col-md-3 col-sm-3 col-xs-12">
                @Html.LabelFor(m => m.MyData, new { @class = "col-form-label" })
            </div>
            <div class="col-lg-9 col-md-9 col-sm-9 col-xs-12">
                @Html.TextBoxFor(m => m.MyData, new { @class = "form-control" })
            </div>
        </div>
        <div class="col-md-6">
            <div class="">
                <button class="btn btn-primary" type="submit">Send</button>
                <button class="btn btn-danger" type="reset"> Clear</button>
            </div>
        </div>
    }

Then clear the form:

    $('.btn:reset').click(function (ev) {
        ev.preventDefault();
        $(this).closest('form').find("input").each(function(i, v) {
            $(this).val("");
        });
    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.