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>');
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(''));
}