Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery clone select doesnt keep value

Tags:

jquery

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();  
like image 678
Tom Avatar asked Jan 05 '11 00:01

Tom


2 Answers

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());
like image 200
Emmett Avatar answered Nov 11 '22 23:11

Emmett


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());
like image 3
Marcus Whybrow Avatar answered Nov 11 '22 21:11

Marcus Whybrow