Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reset a form with the exception of one specific field

Tags:

jquery

I want to clear the form form1 without clearing the textbox textbox1. However the below does not work:

$('#form1').not("input[type=text]#textbox1").trigger('reset');

I could do .html('') for each field except for textbox1, but I do not want to do that since there will be more fields added to the form and therefore the manitaninability is not good.

like image 641
Dylan Czenski Avatar asked Jul 07 '16 16:07

Dylan Czenski


2 Answers

Unfortunately the reset event occurs on the form and affects all form related elements within it. To achieve what you need you could select all your required form elements and set their values manually, eg:

$('#form1').find('input, select, textarea').not("#textbox1").val('');

The element selectors above should be generic enough to cater for additional fields being added to the form dynamically

like image 82
Rory McCrossan Avatar answered Sep 22 '22 20:09

Rory McCrossan


You can first save the value on #textbox1 in a variable, then reset the form normally, and then assign the saved value to #textbox1. This saves the hassle of selecting each and every input type and resetting it separately.

var value1 = $("input#textbox1").val();
$('#form1').trigger('reset');
$("input#textbox1").val( value1 );
like image 20
Mohit Bhardwaj Avatar answered Sep 22 '22 20:09

Mohit Bhardwaj