15

I am trying to clone a feildset then submit the contents of inputs and selects using serialize. It is working properly however select doesn't keep its value. I have tried several methods I have found but nothing seems to work. Here is how I am cloning and setting the current data.

How can I keep the value of select when cloning?

 $('body').append('<form id="form-to-submit" style="visibility:hidden;"></form>');  
var fieldsetName = $this.parents('.fieldsetwrapper');  
$('#form-to-submit').html($(fieldsetName).clone());  
var data = $('#form-to-submit').serialize();  
3
  • 1
    Instead of cloning the entire fieldset, why not just serialize the data from the fieldset as a JS object when you want, and use that object to restore later (if that's what you're after). Or have I misunderstood the reason that you are cloning? Commented Jan 5, 2011 at 0:18
  • 1
    It's not a good idea to use .html() with .clone(). Clone gives elements, while .html() expects a string. Use the elements directly: $('#form-to-submit').empty().append($(fieldsetName).clone()); Commented Jan 5, 2011 at 0:39
  • same issue is on prototype library Commented May 20, 2014 at 22:30

3 Answers 3

30

The option element maintains its current selectedness with the selected javascript property (not to be confused with the selected attribute, which corresponds to default selectedness).

Since jQuery's clone doesn't clone the current selectedness (http://bugs.jquery.com/ticket/1294) , you'll have to do it manually:

$('#form-to-submit').html($(fieldsetName).clone());
$('#form-to-submit select').val($('.fieldsetwrapper select').val());
Sign up to request clarification or add additional context in comments.

3 Comments

Hi, this is a really interesting solution, i didn't know that jQuery could find alone where to place the selected values. Is there a way to use this form an entire form and not only select elements ? Do you know a link that speak about this ? Thanks
I found that this solution didn't work for multiple select menus if they had different direct parents. Instead I had to loop over "each" of the selects and match it with the corresponding select in the original with the ".eq" method. Just a quick note for anyone skimming through! :)
@Casey, I whish you had elaborated with a piece of code :) You'd still have to get the index of the original select right? Or is there another magic function that does this? :-)
3

You should be able to set the value of the new select element to the value of the old one:

$('#form-to-submit select').val($('.fieldsetwrapper select').val());

Comments

0

In my case I was using

$('<form></form>').html($('.myDiv').clone())

and fixed the issue by adding another form to the markup

$('.myForm').serialize()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.