Search code examples
javascriptjqueryformsinputhidden

jquery: need some function that will empty all the <input> value attribute (that inside form) except inputs that type is hidden


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>


Solution

  • 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>