Ver Mensaje Individual
  #2 (permalink)  
Antiguo 05/07/2015, 08:38
nilburcion
 
Fecha de Ingreso: diciembre-2011
Mensajes: 98
Antigüedad: 12 años, 4 meses
Puntos: 29
Respuesta: Pasar options de 2 select a un solo select sin que se repitan

Aquí tienes, primero se pasan todas las "options" de select1 a select3 y luego haces lo mismo con select2 pero con la diferencia que en el segundo bucle recorres también select3 para comprobar que esa "option" en particular no esté ya en select 3:

Código Javascript:
Ver original
  1. function boton_onclick() {
  2.     var select1 = document.getElementById("select1");
  3.     var select2 = document.getElementById("select2");
  4.     var select3 = document.getElementById("select3");
  5.  
  6.     select3.options.length = select2.options.length;
  7.  
  8.     for (i = 0; i < select1.options.length; i++) { //Recorre select1
  9.         select3.options[i].value = select1.options[i].value;
  10.         select3.options[i].text = select1.options[i].text;
  11.     }
  12.  
  13.     for (i = 0; i < select2.options.length; i++) { //Recorre select2
  14.         var added = false;
  15.         for (j = 0; j < select3.options.length; j++) { //Recorre select3 para comprobar que la "option" que vamos a añadir no se encuentre en select3
  16.             if (select2.options[i].value == select3.options[j].value) {
  17.                 if (select2.options[i].text == select3.options[j].text) {
  18.                     added = true;
  19.                 }
  20.             }
  21.         }
  22.  
  23.         if (!added) {
  24.             select3.options.length++;
  25.             select3.options[select3.options.length-1].value = select2.options[i].value;
  26.             select3.options[select3.options.length-1].text = select2.options[i].text;
  27.         }
  28.     }
  29.  
  30. }