Virajsinh

jQuery Form Input Element Value Reset All Elements

Oct 27th, 2025
448
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 0.77 KB | Source Code | 0 0
  1. <script type="text/javascript">
  2.     $('#form_reset_btn').on('click', function () {
  3.         let $form = $(this).closest('.card').find('form');
  4.  
  5.         // Reset the whole form
  6.         $form[0].reset();
  7.  
  8.         // Clear inputs manually, except those with .ignore_reset
  9.         $form.find('input:not(.ignore_reset), textarea:not(.ignore_reset)').each(function () {
  10.             $(this).val('');
  11.         });
  12.  
  13.         // Reset selects, excluding those with .ignore_reset
  14.         $form.find('select:not(.ignore_reset)').each(function () {
  15.             $(this).val('').trigger('change'); // Also works with select2
  16.         });
  17.  
  18.         $form.find('select.ignore_reset').each(function () {
  19.             if ($(this).attr('data-default_value')) {
  20.                 var value = $(this).attr('data-default_value');
  21.                 $(this).val(value).trigger('change');
  22.             }
  23.         });
  24.     });
  25. </script>
Advertisement
Add Comment
Please, Sign In to add comment