Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

197
Vistas
Variable inside loop inside succes: in ajax call JS

Hello I've been working all day on a solution but haven't figured it out. I have a $.ajax call of which I'm looping through and printing out the values I want. That has been done, my problem is a var array that I have assigned however not being able to manipulate it before displaying it. Inside the success function I start a for loop and assign the var array values, If I don't manipulate them the program continues, however when I add some manipulation it gets stuck. I'm trying to replace \s chars for _ chars but no type of manipulation o the titl[i] has worked. I'm fairly new to JS so there might be something I'm missing, would really aprecciate some insight! cheers.

  success: function (x) {
   var titl = [];
   var len = x.query.search.length;

   $('.entries').html("");


   for (var i = 0; i < len; i++) {

    titl[i] = x.query.search.title.split(' ').join('_');
    //want to manipulate titl[i] here *************************
     $('.entries').append('<div class="row"><div class="col-md-12">' +  x.query.search[i].title + '<br>' + x.query.search[i].snippet + '</div></div>'); 
about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Please check the code below with comments in it.

success: function (x) {
    var titles = [], // Do you need them as an array?
        search = x.query.search,
        searchItem,
        title = '',
        len = search.length,
        $entries = $('.entries'), // Keep the reference in a variable
        html = [];

    // You can just remove all content of entries
    $entries.empty();

    for (var i = 0; i < len; i++) {
        // Missing the search index?
        searchItem = search[i];

        // Use push instead and keep title as reference so you can use later on
        title = searchItem.title.split(' ').join('_');
        titles.push(title);

        // Don't append just yet
        html.push('<div class="row"><div class="col-md-12">');
        html.push(title);
        html.push('<br>');
        html.push(searchItem.snippet);
        html.push('</div></div>');
    }

    // Instead append, just use HTML
    $entries.html(html.join(''));
}
about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda