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

138
Visualizações
get two Elements by Id in same js function

I have two dropdowns which I want to fill from the same array which is passed to a function like this

html

<button onclick="popu(<%= JSON.stringify(coun) %>);">blue</button>

<form>
<fieldset>

    <select id="selectNumber" class="selectpicker1" name="selectpicker1">
      <option>Choose a number</option>
    </select>

    <select id="selectNumber2" class="selectpicker2" name="selectpicker2">
      <option>Choose a number</option>
    </select>
    
  <input type="submit" id="submit-form" />
  </fieldset>

popu.js function

function popu(coun) {
    var select = document.getElementById("selectNumber");
    var select2 = document.getElementById("selectNumber2");

    for(var i = 0; i < coun.length; i++) {
        var opt = coun[i];
        var el = document.createElement("option");
        el.textContent = opt;
        el.value = opt;
        select.appendChild(el);
        select2.appendChild(el);
    }
   
}

But only one drop-down is getting filled.

So, I came across a jquery answer but I don't know jquery yet, and made this which also doesn't work

$(function popu(coll) {
    var elements = $('[id^="selectNumber"]');
    
    $.each(elements, function(index, value){
        var select = document.getElementById(elements[index]);

        for(var i = 0; i < coun.length; i++) {
            var opt = coun[i];
            var el = document.createElement("option");
            el.textContent = opt;
            el.value = opt;
            select.appendChild(el);
          //  select2.appendChild(el);
        }
     });         
 });
about 4 years ago · Juan Pablo Isaza
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