Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Form Resetting is not working using jquery

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?

like image 265
Kichu Avatar asked Jan 20 '12 05:01

Kichu


People also ask

How we can reset form in jQuery?

JQuery doesn't have a reset() method, but native JavaScript does. So, we convert the jQuery element to a JavaScript object. JavaScript reset(): The reset() method resets the values of all elements in a form (same as clicking the Reset button).

How do I reset form data?

reset() method restores a form element's default values. This method does the same thing as clicking the form's <input type="reset"> control. If a form control (such as a reset button) has a name or id of reset it will mask the form's reset method. It does not reset other attributes in the input, such as disabled .

What does Reset () do in JavaScript?

In JavaScript, the reset() method does the same thing as the HTML reset button. It is used to clear all the values of the form elements. It can be used to set the values to default. It does not require any parameter values and also does not return any value.


2 Answers

In jQuery

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

in Javascript

document.getElementById('frm_silder').reset()
like image 61
Sameera Thilakasiri Avatar answered Sep 22 '22 11:09

Sameera Thilakasiri


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

like image 36
xbonez Avatar answered Sep 22 '22 11:09

xbonez