Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

305
Views
JS append not working when using a for loop

I have this code:

success: function (result) {
   var textoTipos = $('.cargarTextosTipo');
                      
   for (var selecttipos of textoTipos) {
       selecttipos.append(`<option value="0">Hello this is a test</option>`);

   // some code

   }

}

My problem is that inside loop selecttipos.append(`<option value="0">Hello this is a test</option>`); is not working and it is not appending to a select component but this way worked but this is not the way I am looking for:

 success: function (result) {
    var textoTipos = $('.cargarTextosTipo');
   textoTipos.append(`<option value="0" > Hello this is a test </option>`);

}

How do I make it work using a loop? this is the way I want because I have to go through each select node and do some stuff later rather than applying to all select stuff.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

When looping textoTipos you get plain HTML elements. You should wrap selecttipos to be jQuery object: $(selecttipos)

function success(result) {
   var textoTipos = $('.cargarTextosTipo');
                      
   for (var selecttipos of textoTipos) {
       $(selecttipos).append(`<option value="0">Hello this is a test</option>`);

   // some code

   }
}

$(document).ready(success);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<select class="cargarTextosTipo"></select>
<select class="cargarTextosTipo"></select>
<select class="cargarTextosTipo"></select>
<select class="cargarTextosTipo"></select>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!