Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

copy/Clone dropdown list with selected option in jquery

How can I clone dropdown list(combobox) with selected option?

jquery .clone method is not working in firefox for selected option.

I have a div having different controls. I have to copy entire div to a variable something like this

var $orginalDiv = $('#myDiv');
var $clonedDiv = $orginalDiv.clone();

$clonedDiv.find('select').each(function() {


....Something do here for assigning selected options from original div ..

            });

Let me know how can we get it done and it must be worked in FireFox.

like image 617
Brij Avatar asked Oct 05 '10 07:10

Brij


1 Answers

var $orginalDiv = $('#myDiv');
var $clonedDiv = $orginalDiv.clone();

//get original selects into a jq object
var $originalSelects = $orginalDiv.find('select');

$clonedDiv.find('select').each(function(index, item) {

     //set new select to value of old select
     $(item).val( $originalSelects.eq(index).val() );

});

Try it here at jsfiddle

like image 189
redsquare Avatar answered Oct 16 '22 13:10

redsquare