views:

64

answers:

1

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.

+2  A: 
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

redsquare
Getting error: $originalSelects is not defined
Brij
@brzdotnet sorry typo, corrected
redsquare
+1 I wasn't sure if index would work if `select`'s are not siblings, but it [works](http://jsfiddle.net/kvzam/)!
Reigel
+1 Now it works.Nice Ans, shows Good experience with jQuery. Thanks
Brij