Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

168
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda