1

I have a bunch of inputs, i'd like to call reset but there are all outside of a form. I tried calling reset on input and textarea with no luck.

Is there a similar function i can use?

2
  • why do you have input controls outside of a form? is that even W3C valid? Commented Nov 13, 2010 at 4:23
  • @vol7ron: It is completely legal. Why? JS magic! and ajax too if you need it ;) Commented Nov 14, 2010 at 7:22

3 Answers 3

5

Keep in mind that the form RESET actually doesn't clear all fields, it will reset a form's default values back to default as well, so a better approach might be the following:

$('#the_form').trigger('reset'); 

Perhaps another approach:

// capture all existing values var arr = []; $(':input').each(function(i, e) { arr.push($(e).val()); }); // custom function to reset all values to initially captured values function my_reset() { $(':input').each(function(i, e) { $(e).val(arr[i]); }); } 

The above approach blindly targets all fields, if you have a way to better target them, you should definitely use what you can.

Additionally, this approach stores all the fields in order, so if you have dynamically generated fields, then this solution would have to be revised.

Sign up to request clarification or add additional context in comments.

1 Comment

did you read his question? the inputs are not inside his form, so form.reset() won't work.
1

My approach (second to putting them in a form...) would be to, onload, map the default values to each input id or name, and then create a reset method that just iterates that collection, get by id and set to default...

1 Comment

Indeed - built-in reset is defined on a form, not on form elements. Your approach is what I would use.
1

Do you want to reset or just to clear the inputs? Reset would be more complicated, but clearing in your case is easy:

HTML:

 <input type="text"/> <textarea></textarea> <button id="resetBtn">Reset</button> 

JS:

$("#resetBtn").click(function(){ $("input, textarea").val(""); }); 

Comments