Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript: How to copy all options from one select element to another?

Tags:

How can I copy all options of one select element to another? Please give me the easiest way, I'm allergic to looping.

Please help me. Thanks in advance!

like image 632
dpp Avatar asked Jun 01 '11 09:06

dpp


2 Answers

One of the easiest ways without looping, is using jquery (select1 = id of select 1, select2 = id of select 2):

$('#select1 option').clone().appendTo('#select2'); 

Without jquery:

var select1 = document.getElementById("select1"); var select2 = document.getElementById("select2"); select2.innerHTML = select2.innerHTML+select1.innerHTML; 
like image 70
manji Avatar answered Oct 03 '22 23:10

manji


html:

<select id="selector_a">     <option>op 1</option>     <option>op 2</option> </select>  <select id="selector_b">     <option>op 3</option>     <option>op 4</option> </select> 

javascript:

var first = document.getElementById('selector_a'); var options = first.innerHTML;  var second = document.getElementById('selector_b'); var options = second.innerHTML + options;  second.innerHTML = options; 
like image 22
helle Avatar answered Oct 03 '22 22:10

helle