0

I have been doing some research on a good way to clear a jQuery form.

I have a couple of ideas but I want to know if someone has a better way.

I know about using reset, but sometimes it is necessary to set a field back to a particular value that may not be accomplished by reset. (ie. if a form starts with some pre-filled data and you want to clear it)

I have read some discussion here: Resetting a multi-stage form with jQuery

But I have another idea that I will post as an answer. Please let me know what you think and if you have a better solution.

More Information:

For example I have an address form. It has a couple of inputs, selects, radios, etc. along with a copy into new button and a clear button. By default I want to have this particular radio selected to option 1.

<div id = "AddressList">
<div class = "Address">
<input type="radio"/> --obviously with some options here
<button>copy into new</button>
</div>
</div>

The user selected option two and clicks copy into new. So I say something like on the click of the click of the copy into new:

$("#addressList").append($(this).closest("#Address").html());

In the second address the secon radio button is selected. The user selects the third radio button. At this point clicking the reset button would reset the input back to option two not option one.

Community
  • 1
  • 1
kralco626
  • 8,456
  • 38
  • 112
  • 169
  • If the form starts with pre-filled data, I would think that data would be populated with placeholder="my text". If that's the case, clearing all inputs should be fine, right? – Stefan Kendall Dec 20 '10 at 16:45
  • If I have a form that starts with a set of data, and I want to reset it to a different set of data this will not work? right? – kralco626 Dec 20 '10 at 16:46
  • For example my form has a radio set. My "reset" button would set this radio button to the first item selected. I load some data from the database and give the suer the form to edit and the database says item 2 is selected. I want the reset button to have the radio button go back to having the first item selected. But reset will not do this. right? – kralco626 Dec 20 '10 at 16:48

1 Answers1

0

My solution would be to add the attribute called clear.

Then say $([clear]).val($(this).atrr('clear'))

kralco626
  • 8,456
  • 38
  • 112
  • 169