Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reset a particular form field using jQuery

I want to know how to reset a particular form field using jQuery.

I'm using the folowing function:

function resetForm(id) {
    $('#'+id).each(function(){
        this.reset();
    });
}

but it is resetting all the fields. Is there any way to reset specific fields using jQuery or JavaScript? I want to reset only a particular field.

like image 419
Manish Jangir Avatar asked Dec 29 '11 12:12

Manish Jangir


People also ask

How do you reset a form field?

You can easily reset all form values using the HTML button using <input type=”reset”> attribute. Clicking the reset button restores the form to its original state (the default value) before the user started entering values into the fields, selecting radio buttons, checkboxes, etc.

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 can I delete form field after submit in jQuery?

With jQuery, you can easily clear HTML form fields. For this, jQuery: reset Selector is used. This will clear the HTML form fields.

What is reset in jQuery?

In jQuery, the :reset selector is used to select the elements of type reset. It selects the input elements and buttons with type = reset. If we write input:reset, the selector will only select the input elements with type reset.


2 Answers

function resetForm(id) {
    $('#' + id).val(function() {
        return this.defaultValue;
    });
}

example without using id:

http://jsfiddle.net/vYWdm/

like image 183
Esailija Avatar answered Oct 06 '22 01:10

Esailija


The reset() method affects the whole form, by design, to reset only particular input elements, assuming that there's a Reset previous inputelement after eachinput`:

$('button.reset').click(
    function(){
        var input = $(this).prev('input:first');
        input.val(''); // assuming you want it reset to an empty state;
    });

JS Fiddle demo

To reset the input to its original state, then first I'd recommend storing the original value in a data-* attribute for recollection:

$('input').each(
    function(){
        $(this).attr('data-originalValue',$(this).val());
    });

$('button.reset').click(
    function(){
        var input = $(this).prev('input:first');
        input.val(input.attr('data-originalValue'));
    });

JS Fiddle demo

Given HTML similar to the following (in which the input elements are grouped together):

<form action="#" method="post">
    <fieldset>
        <input value="something" />
        <button class="reset">Reset the input</button>
    </fieldset>
    <fieldset>
        <input type="checkbox" name="two"  />
        <input type="checkbox" name="two" checked />
        <input type="checkbox" name="two"  />
        <button class="reset">Reset the input</button>
    </fieldset>
    <fieldset>
        <input type="radio" name="three" checked />
        <input type="radio" name="three" />
        <button class="reset">Reset the input</button>
    </fieldset>
</form>

The following jQuery will reset the input elements back to their page-load state:

$('button.reset').click(
    function () {
        $(this).prevAll('input').val(function(){
            switch (this.type){
                case 'text':
                    return this.defaultValue;
                case 'checkbox':
                case 'radio':
                    this.checked = this.defaultChecked;
            }
        });
    });

JS Fiddle demo.

References:

  • attr().
  • prev().
  • reset()
  • val().
like image 36
David Thomas Avatar answered Oct 05 '22 23:10

David Thomas