javascript - How to clone select options without cloning the select -


say have select:

<select id="myselect">  <option value="1">first</option>  <option value="2">second</option>  <option value="3">third</option> </select> 

i clone options without having clone select. possible? cloning select not possible in situation.

i thought of doing this:

$('#myselect').html().clone(); 

but did not work.

update:

i have select change on time. have second select in modal. when modal called, need update modal's select same options first modal. both selects have different attributes, can't clone select.

the straightforward way copy options primary select secondary select use html() follows:

var options = $('#myselect').html(); $('#myselect2').html(options); 

or in 1 line:

$('#myselect2').html($('#myselect').html()); 

if want manipulate options before copying other select, use clone() array of individual select options jquery objects , alter array before copying entires other select. however, based on updated requirement of copying options 1 select another, using clone overkill. how it:

var optionsarr = $('#myselect option').clone(); console.log(optionsarr); // see contents // manipulate , populate other select needed 

Popular posts from this blog