0

I need to empty all the values, all the checked boxes and all the selected items (reset the values inside ) except the inputs with type="hidden" attribute.

<form id="formname">
<input type="text" value="1232423"/>
<input type="password" value="abcdefgh"/>
<input type="file"/>
<textarea>some inputed text</textarea>
<input type="checkbox" checked="checked"  />
<select>
<option>a</option>
<option selected="selected">b</option>
<option>c</option>
</select>

<input type="hidden" value="storeddata1"/>
<input type="hidden" value="storeddata2"/>
<input type="hidden" value="storeddata3"/>
</form>
legopart
  • 21
  • 3

2 Answers2

1

Try this (https://codepen.io/AlibiGhazi/pen/XWddyPR)

$(':input','#formname')  
  .not(':hidden')
  .val('')
  .prop('checked', false)
  .prop('selected', false);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="formname">
<input type="text" value="1232423"/>
<input type="password" value="abcdefgh"/>
<input type="file"/>
<textarea>some inputed text</textarea>
<input type="checkbox" checked="checked"  />
<select>
<option>a</option>
  
  
<option selected="selected">b</option>
<option>c</option>
</select>

<input type="hidden" value="storeddata1"/>
<input type="hidden" value="storeddata2"/>
<input type="hidden" value="storeddata3"/>
</form>
Alibi Ghazi
  • 174
  • 7
0

You can also make use of form.reset(). This method when called on a form element, resets all its children to have the initial value that were given to these children.

Kumar Aman
  • 271
  • 2
  • 7