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

158
Visualizações
Filling up one dropdown with same value of the other

Currently I have 2 dropdown boxes with the same options. These options are all filled from the backend. Now I want it so that whenever I choose 1 option in the dropdown box, the other dropdown also automatically chooses the same value and this works both ways. The following is my dropdown box code:

        <select name="lead_status" id="leadstatus" class="form-control">
          <option value="">Select</option>
          <?php foreach($leadstatus as $key => $status): ?>
          <option value="<?php echo $key; ?>" <?php echo $key==$lead->lead_status?'selected="selected"':'' ?>><?php echo $status; ?></option>
          <?php endforeach; ?>
        </select>
        <select name="lead_status" id="leadstatus2" class="form-control">
          <option value="">Select</option>
          <?php foreach($leadstatus as $key => $status): ?>
          <option value="<?php echo $key; ?>" <?php echo $key==$lead->lead_status?'selected="selected"':'' ?>><?php echo $status; ?></option>
          <?php endforeach; ?>
        </select>

I've tried creating a function for onclick where it creates a check saying:

if (document.getElementById('leadstatus').value == '1')
  document.getElementById("leadstatus2").value = '1';

But this wont work since I have a lot of data in the dropdown lists and it is dynamic.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Since the question mentions... ...the other dropdown also automatically chooses the same value and this works both ways...

Try the following, ..

const leadStatusList = document.getElementById('leadstatus');
const leadStatus2List = document.getElementById('leadstatus2');

const setDropdownValue = (event) => {
  leadStatusList.value = event.target.value;
  leadStatus2List.value = event.target.value;
}

leadStatusList.onchange = setDropdownValue;
leadStatus2List.onchange = setDropdownValue;
about 4 years ago · Juan Pablo Isaza Relatório

0

Simply set the second dropdown's selectedIndex property to that of the first dropdown:

let box1 = document.getElementById("box1");
let box2 = document.getElementById("box2");
box1.addEventListener("change", () => {
  box2.selectedIndex = box1.selectedIndex;
});
box2.addEventListener("change", () => {
  box1.selectedIndex = box2.selectedIndex;
});
<select id="box1">
  <option value="1">Option 1</option>
  <option value="2">Option 2</option>
  <option value="3">Option 3</option>
  <option value="4">Option 4</option>
  <option value="5">Option 5</option>
</select>
<select id="box2">
  <option value="1">Option 1</option>
  <option value="2">Option 2</option>
  <option value="3">Option 3</option>
  <option value="4">Option 4</option>
  <option value="5">Option 5</option>
</select>

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