There is such a structure:

<div class="first"> <input type="text" size="40" id="txt"> <input type="radio" name="test" value="1" id="rad"> <select> <option>Пункт 1</option> <option>Пункт 2</option> </select> </div> <div class="second"> ... </div> 

How to write an event that would clear all the filled fields. If manually it will be -

 $('#txt').val(''); $(':input','#rad').removeAttr('checked'); ... 

etc.

But what if many items?
Is it possible to somehow clear by the parent class?

  • $('родитель').find(':input','#rad').removeAttr('checked'); - so, what? - lexxl
  • But the elements are different for you. Well, here you have written an example, remove checked there, there is text ... one command for different elements is unlikely to work, I think so ... - Denis Bubnov
  • Many similar or different items? - alexoander
  • radio, text, checkbox, and option. Somewhere in ~ 10 fields of each element, in id I am shocking to write) - Vlad Shkuta

2 answers 2

If you need to reset all the filled fields of the form, you can refer to this form:

 $('.form')[0].reset(); 

Or you can specify some specific elements:

 $(':input','.form') .not(':button, :submit, :reset, :hidden') .val('') .removeAttr('checked') .removeAttr('selected'); 
  • And you can reset all the elements of the form, except for one? $('#form-search')[0].reset(); - Vlad Shkuta
  • It is impossible. This is the native javascript method. But you can, for example, save the entered value before dropping it, and then substitute it: var text = $('textarea').val(); $('form')[0].reset(); $('textarea').val(text); var text = $('textarea').val(); $('form')[0].reset(); $('textarea').val(text); - lexxl
 $("div").children("input[type='radio']").removeAttr('checked'); $("div").children(".checkboxClass").removeClass('checked'); $("div").children("input[type='text']").val(""); 

Like that, for each of the types of elements has its own behavior. But anyway faster than all the elements to sort out. The div is the parent element.