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

91
Visualizações
set value array in all inputs with same tag class

I'm trying to do loop over all tag with same className and get their value:

var quantity = [];
$(".add_more_items").each(function(){
    quantity.push($(this).val());
});

this is a result, for example:

['1', '9', '1']

but my problem is I'm trying to set value from this array to other input with same class:

$.each(quantity, function(index, val){
    $(".items_final").val(val);
});

but always set in all inputs last value from my array, i don´t know what I'm doing wrong.

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

0

Use an index assuming there is 1 to 1 mapping between the fields

const $final = $(".items_final");
$(".add_more_items").each(function(i, item) {
  $final[i].value = item.value; // or $final.eq(i).val(item.value)
});

const $final = $(".items_final");
$(".add_more_items").each(function(i, item) {
  $final[i].value = item.value; // or $final.eq(i).val(item.value)
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<h3>Add more</h3>
<input type="text" value="1" class="add_more_items" />
<input type="text" value="2" class="add_more_items" />
<input type="text" value="3" class="add_more_items" />
<input type="text" value="4" class="add_more_items" />
<hr/>
<h3>final</h3>
<input type="text" value="" class="items_final" />
<input type="text" value="" class="items_final" />
<input type="text" value="" class="items_final" />
<input type="text" value="" class="items_final" />

Also this is useful:

const quantity = $(".add_more_items").map(function(){
  return this.value; // or $(this).val()
}).get();
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