I’m looking to dynamically (from data provided via whatever…jSon, XML, a static array, etc.) build a series of divs that in the end contain anchor tags (or buttons) with images inside of them.
This is what I have so far:
$(document).ready(function () {
let testButtonEntries = [
{ button_title: 'Employee Button Title', button_alt_text: 'Employee Alt Text', button_image: 'emp_button_title', link: 'insertsomelinkhere' },
{ button_title: 'Employee Button Title', button_alt_text: 'Employee Alt Text', button_image: 'emp_button_title', link: 'somelinktosomewhere.com/' },
{ button_title: 'Employee Button Title', button_alt_text: 'Employee Alt Text', button_image: 'emp_button_title', link: 'somelinktosomewhere.com/' },
{ button_title: 'Employee Button Title', button_alt_text: 'Employee Alt Text', button_image: 'emp_button_title', link: 'somelinktosomewhere.com/' },
{ button_title: 'Employee Button Title', button_alt_text: 'Employee Alt Text', button_image: 'emp_button_title', link: 'somelinktosomewhere.com/' },
{ button_title: 'Employee Button Title', button_alt_text: 'Employee Alt Text', button_image: 'emp_button_title', link: 'somelinktosomewhere.com/' },
{ button_title: 'Employee Button Title', button_alt_text: 'Employee Alt Text', button_image: 'emp_button_title', link: 'somelinktosomewhere.com/' },
{ button_title: 'Employee Button Title', button_alt_text: 'Employee Alt Text', button_image: 'emp_button_title', link: 'somelinktosomewhere.com/' },
{ button_title: 'Employee Button Title', button_alt_text: 'Employee Alt Text', button_image: 'emp_button_title', link: 'somelinktosomewhere.com/' },
{ button_title: 'Employee Button Title', button_alt_text: 'Employee Alt Text', button_image: 'emp_button_title', link: 'somelinktosomewhere.com/' },
{ button_title: 'Employee Button Title', button_alt_text: 'Employee Alt Text', button_image: 'emp_button_title', link: 'somelinktosomewhere.com/' },
{ button_title: 'Employee Button Title', button_alt_text: 'Employee Alt Text', button_image: 'emp_button_title', link: 'somelinktosomewhere.com/' },
{ button_title: 'Employee Button Title', button_alt_text: 'Employee Alt Text', button_image: 'emp_button_title', link: 'somelinktosomewhere.com/' },
{ button_title: 'Employee Button Title', button_alt_text: 'Employee Alt Text', button_image: 'emp_button_title', link: 'somelinktosomewhere.com/' },
{ button_title: 'Employee Button Title', button_alt_text: 'Employee Alt Text', button_image: 'emp_button_title', link: 'somelinktosomewhere.com/' },
{ button_title: 'Employee Button Title Last', button_alt_text: 'Employee Alt Text Last', button_image: 'emp_button_title_last', link: 'insertsomelinkhere.org' }
];
for (let sections = 0; sections <= testButtonEntries.length % 15; sections++) {
$('<div class="carousel-item">')
.append($('<div class="d-flex flex-wrap"/>').append(function () {
for (let i = 0; i < 15; i++) {
return $('<div/>')
.append($('<a/>', {
href: testButtonEntries[i].link,
title: testButtonEntries[i].button_title,
target: '_blank'
}).append($('<img/>', {
alt: testButtonEntries[i].button_alt_text,
class: 'img-responsive',
src: 'images/' + testButtonEntries[i].button_image + '.png'
})));
}
}))
.appendTo($('#employee-button-container'));
}
});
It produces what I want BUT (because obviously the return statement is gonna kick back the first result and that’s a wrap) it doesn’t get me 15 "buttons" in the 1st section and a single one in the 2nd section that I’m striving for.
2
Answers
Here is the answer I was looking for (just need to sleep on it lol)
We can use slice method to divide/split the main array into separate array chunks(with 15 elements each) and use map to build the HTML