Home jquery How do I clear form fields with jQuery?

How do I clear form fields with jQuery?

Author

Date

Category

There is a page with a number of different elements. We need to return them to their original state (as when the page was loaded). It’s ugly to do this:

$ ('# name'). val ('');

Answer 1, authority 100%


Answer 2, authority 90%

If there is a form:

& lt; form id = "myform" & gt;
...
& lt; / form & gt;

then you can clear it using javascript:

document.getElementById ('myform'). reset ();

or jQuery:

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

Added from comment.

Then as an option

$ (function () {
  $ (itemset) .each (function () {
    $ (this) .data ('defvalue', this.value);
  });
});

And then call

$ (element_set) .each (function () {
  $ (this) .val ($ (this) .data ('defvalue'));
});

Answer 3, authority 80%

$ ("form # id_form"). trigger ('reset');

Answer 4, authority 40%


Answer 5, authority 40%

Try this option:

& lt; script & gt;
  jQuery (document) .ready (function () {
   $ ('input'). val ('');
  });
& lt; / script & gt;

Answer 6, authority 40%

$ (". reset-form-button"). click (function () {
  $ ("form") [0] .reset ();
});

Answer 7, authority 20%

$ (‘input [name * = “field_name”]’). val (”);


Answer 8, authority 20%

Try this:
$ ("* [tag = 'tag']"). val (""). attr ("checked", false);

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions