I have a jquery loop that is creating a few titles with a few buttons under each title. I need to add styling to the titles and to the buttons. Right now they are being created in the loop as html elements. How would I go about adding a CSS class while looping instead of creating these new html elements such as <h3>
and <button>
? Here is a screenshot of what I am aiming to accomplish. Right now I just have titles with buttons unstyled displaying underneath it, https://gyazo.com/2ab440e2792fc55dc32cdd39823ff0a9
let response = [{
"name": "Study",
subSection: "Education"
}, {
"name": "Classes",
subSection: "Education"
},
{
name: "Society",
subSection: "Social"
}
];
let res = response.reduce((obj, item) => {
obj[item.subSection] = obj[item.subSection] || [];
obj[item.subSection].push(item.name);
return obj;
}, {});
// get values array and iterate
Object.keys(res).forEach(function(k) {
// generate h3 ith subSection value and append
$('#container').append(
$('<h3>', {
text: k
})
)
// generate buttons and append
.append(res[k].map(v =>
$('<button>', {
text: v
})
))
})