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

196
Visualizações
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 Respostas
Responde à pergunta

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 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