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
Loop onchange function to manipulate the target class name

I used php and jquery. I have to generate multiple select tag depends on the value of condition. I need to manipulate both html,php and and jquery by using loop but I can't find the answer for this to work.

php html code... I like to populate multiple select tag with different classname

 <?php $count = 2; ?>
 <div id="comments" hidden><?php echo htmlspecialchars($count); ?></div>
 <?php for($i = 1; $i <= $count; $i++){ ?>
 

   <input type="text" readonly="readonly" class="resone<?php echo $i; ?>" id="resone">
       <select name="artist_1" class="part<?php echo $i; ?>" id="part">
        <option value="" disabled selected>Select Category</option>
        <option value="1">medicine</option>
        <option value="2">shoes</option>
       </select>
<?php }?>

jquery. I like to put the onchange function into the loop to manipulate the target class value

     <script>
           $(function() {
              var count = document.getElementById("comments").textContent;
                 for (var i = 1; i <= count; i++) {  
                $('.part' + i).change(function() {
                                            
                  var display = $('.part' + i).find(":selected").val();
                  $('.resone' + i).val(display);
    
                 })
           }
          
     })
</script>

I hope you could help me with this one.

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

0

You are overcomplicating things and abusing the class by adding a counter to it.

Here is my suggestion - you can actually ignore the IDs and the script does not care about the count, just about how many ".part" there are

$(function() {
  $(".part").on("change", function() {
    $(this).prev(".resone").val(this.value);
  })
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="text" readonly="readonly" class="resone" id="resone0">
<select name="artist_1" class="part" id="part0">
  <option value="" disabled selected>Select Category</option>
  <option value="1">medicine</option>
  <option value="2">shoes</option>
</select>

<hr/>

<input type="text" readonly="readonly" class="resone" id="resone1">
<select name="artist_1" class="part" id="part1">
  <option value="" disabled selected>Select Category</option>
  <option value="1">medicine</option>
  <option value="2">shoes</option>
</select>

PHP: - the IDs are not even used

<?php for($i = 1; $i <= $count; $i++){ ?>
  <input type="text" readonly="readonly" class="resone" id="resone<?php echo $i; ?>">
   <select name="artist_1" class="part" id="part<?php echo $i; ?>">
    <option value="" disabled selected>Select Category</option>
    <option value="1">medicine</option>
    <option value="2">shoes</option>
   </select>
<?php }?>
about 4 years ago · Juan Pablo Isaza Relatório

0

You don't need to use a for loop, just give the select a common class and do like this:

$(function() {
  $('.part').change(function() {
    var display = $(this).val();
    $(this).next('.resone').val(display);
  })
})

Since I can't see the element with the class resone i can't make sure if $(this).next('.resone').val(display) works.

Code example

$(function() {
  $('.part').change(function() {
    var display = $(this).val();
    $(this).next('.resone').val(display);
  })
})
<?php $count = 2; ?>
<div id="comments" hidden>
  <?php echo htmlspecialchars($count); ?>
</div>
<?php for($i = 1; $i <= $count; $i++){ ?>

<select name="artist_1" class="part" >
  <option value="" disabled selected>Select Category</option>
  <option value="1">medicine</option>
  <option value="2">shoes</option>
</select>
<?php }?>

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