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();
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());
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());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With