Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

167
Vistas
Move multi-select list with JavaScript

I'm programming for the first time. I want to write a code that moves multiple selections from a list with the up and down buttons in JavaScript. However, I was able to write something that moves up and down, but it does not work well if I move multiple times. Please tell me how to move multiple selected items up and down.

If you select 3 and 5 and press the up button, you want to do as follows.

<option value = "1"> 1 </ option>
<option value = "2"> 2 </ option>
<option value = "3"> 3 </ option>
<option value = "4"> 4 </ option>
<option value = "5"> 5 </ option>
<option value = "6"> 6 </ option>
↓
<option value = "1"> 1 </ option>
<option value = "3"> 3 </ option>
<option value = "2"> 2 </ option>
<option value = "5"> 5 </ option>
<option value = "4"> 4 </ option>
<option value = "6"> 6 </ option>
<table>
   <tr>
       <td>
           <select id="item-list" size="8" multiple="multiple">
               <option value="1">1</option>
               <option value="2">2</option>
               <option value="3">3</option>
               <option value="4">4</option>
               <option value="5">5</option>
           </select>
       </td>
       <td>
           <input type="button" value="↑" onclick="move('up');">
           <br/>
           <input type="button" value="↓" onclick="move('down');">
       </td>
   </tr>
</table>
function move(act)
{
    var s = document.getElementById("item-list");
    if (s.selectedIndex == -1) return;

    var opt = s.options[s.selectedIndex];


            if (act == 'up')
            {
                if (s.options[s.selectedIndex - 1])
                {
                    s.insertBefore(opt, s.options[s.selectedIndex - 1]);
                }
            }
            if (act == 'down')
            {
                if (s.options[s.selectedIndex + 1])
                {
                    s.insertBefore(opt, s.options[s.selectedIndex + 1].nextSibling);
                }
            }
}
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Your code had several small bugs. i fixed.

function move(act) {  
    var s = document.getElementById("item-list");
  
    if(s.selectedIndex == -1) return;
    
    var opt = s.options[s.selectedIndex];

    var opts = s.options;
  
    for (let i = 0; i < opts.length; i++) {
        if (opts[i].selected){
            console.log(opts[i].value);

        if (act == 'up') {
          if (s.options[s.selectedIndex-1]) {
              s.insertBefore(opt, s.options[s.selectedIndex-1]);
          }
        }
          
        if (act == 'down') {
          if (s.options[s.selectedIndex+1]) {
            s.insertBefore(opt, s.options[s.selectedIndex+1].nextSibling);
          }
        }
      }   
    }
}
<table>
   <tr>
       <td>
           <select id="item-list" size="8" multiple="multiple">
               <option value="1">1</option>
               <option value="2">2</option>
               <option value="3">3</option>
               <option value="4">4</option>
               <option value="5">5</option>
           </select>
       </td>
       <td>
           <input type="button" value="↑" onclick="move('up');">
           <br/>
           <input type="button" value="↓" onclick="move('down');">
       </td>
   </tr>
</table>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda