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

194
Views
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 answers
Answer question

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