Dylan Czenski
Dylan Czenski

Reputation: 1365

Reset a form with the exception of one specific field

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.

Upvotes: 6

Views: 5247

Answers (2)

Mohit Bhardwaj
Mohit Bhardwaj

Reputation: 10083

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 );

Upvotes: 2

Rory McCrossan
Rory McCrossan

Reputation: 337560

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

Upvotes: 11

Related Questions