I've created a helper function that builds the list items for a carousel. Ideally I would call this at the bottom of the page and output the html structure on the page. It should receive a url and then fetch the data from the passed url.
Could someone enlighten me as to why the following function is not returning the value to the page?
Javascript
function selectAfterCharacter(item, character) {
return item.substring(item.lastIndexOf(": ") + 1, item.length).trim();
}
function buildCarouselItem (url) {
jQuery.ajax({
url: url,
success: function(result) {
html = jQuery(result);
var title = (html.find("h1#product_title").html());
var productId = (html.find("span#prod_code").html());
var strippedId = selectAfterCharacter(productId, ": ");
var price = (html.find("span.one_price").html());
var Nowprice = (html.find("span.now_price").html());
if (price !== undefined) {
var price = (html.find("span.one_price").html());
} else {
var price = (html.find("span.now_price").html());
}
return(`
<li>
<a href='${url}'>
<img alt='${title}' src='http://www.awebsite.com/${strippedId}/large/${strippedId}.jpg'>
<div class="product-title">
${title}
</div>
<div class="product-price">
<h4>${price}</h4>
</div>
</a>
</li>
`
);
},
});
};
function buildCompleteCarousel (theArray) {
for (var i = 0; i < theArray.length ; i++) {
buildCarouselItem(theArray[i]);
}
}
var LinksArray = [
"http://www.awebsite.com/115812",
"http://www.awebsite.com/115812",
"http://www.awebsite.com/115812"
];
var htmlOutput = buildCompleteCarousel(LinksArray);
$('output').innerHTML = htmlOutput;