-3

Possible Duplicate:
How do I clear all the fields in a variable html content? (preferably with jQuery)

The answer of this question is not find in this question: Blank out a form with jQuery, since I have no form ID to connect to.

I have a text string with a value of a HTML code snippet that has some form elements within, with pre filled values​​. All fields that are found in this code snippet (only) must be cleared/reset.

Community
  • 1
  • 1
Peter Westerlund
  • 741
  • 1
  • 10
  • 36

2 Answers2

1

You could just create a form, populate it with your string, and then reset it, like so:

var myForm = $('<form>').html(your_string).
                find(':input').not(':button, :submit, :reset, :hidden').
                val('').removeAttr('checked').removedAttr('selected');

Then it's just a matter of accessing the innerHTML of myForm.

sdleihssirhc
  • 42,000
  • 6
  • 53
  • 67
  • I can't do that, couse my string is already a part of a form. But it's just not appended to the form in this state. – Peter Westerlund Aug 30 '11 at 17:22
  • @Peter What do you mean it's "already part of a form"? – sdleihssirhc Aug 30 '11 at 17:56
  • I hope you understand that the text string is a variable and not an existing HTML from the page source code. – Peter Westerlund Aug 30 '11 at 18:05
  • @Peter My answer assumes that you have the text string as a variable (that's the `your_string`) part, but it also assumes that that string contains HTML. Is that not the case? – sdleihssirhc Aug 30 '11 at 18:25
  • Yes that is the case. It is a string with HTML. And the string is going to be part of an already existing form. In fact, the string is already a bit from the form, that I want to "recycle" and clear up before it reused. – Peter Westerlund Aug 30 '11 at 18:33
0

Assuming you have a text string like this:

var txt = '<form id="search" action="/search" method="get" autocomplete="off"><div><input autocomplete="off" name="q" type="text" value="sample"></div></form>'
var $form = $(txt);

$form[0].reset();

var resetTxt = $form.html();
Mrchief
  • 75,126
  • 20
  • 142
  • 189