I have successfully implemented your code and made some enhancements from line 86 to 120.
const container = document.querySelector('.main');
const topSection = document.createElement('DIV');
const bottomSection = document.createElement('DIV');
const headline = document.createElement('H1');
headline.textContent = 'Main Heading';
container.before(headline);
var items = [
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
];
const addTopSection = () => {
topSection.textContent = 'Top Section Title';
topSection.classList.add('top-section');
topSection.innerHTML = '<div class="innerContent">Inner Content</div>';
topSection.setAttribute(
"style", "font-size: 32px; font-style: italic; background-color:#ff0000; color: white; width: 100%; height: 200px; padding: 20px; font-family: sans-serif");
container.append(topSection);
}
const addBottomSection = () => {
bottomSection.textContent = 'Bottom Section Title';
bottomSection.classList.add('bottom-section');
bottomSection.innerHTML = '<div class="innerBlock">Inner Block</div>';
bottomSection.setAttribute(
"style", "font-size: 32px; font-style: italic; background-color:#000000; color: white; width: 100%; height: auto; padding: 20px; font-family: sans-serif");
container.append(bottomSection);
}
const generateListItems = (elements) => {
let list = '';
for (let i = 0; i < elements.length; i++) {
list += `<li>${elements[i]}</li><button type='button' class='removeItem'>Remove Item</button>`;
}
return list;
}
const arrangeContent = () => {
const layout = addTopSection() + addBottomSection();
}
arrangeContent();
const addToItemList = () => {
const addButton = document.getElementById('addButton');
const inputField = document.getElementById('input').value;
items.push(inputField);
updateHTML(items);
}
const removeLastItem = () => {
items.pop(items);
updateHTML(items);
}
const removeFromSpecificIndex = (index) => {
// One-liner code to remove an element in an array, feel free to ask if you need clarification.
(index > -1) ? items.splice(index, 1) : false;
updateHTML(items);
}
const updateHTML = (items) => {
let finalHtml = `
<ul>
${generateListItems(items)}
</ul>
<input type='input' id="input" required></input><button type='button' id='addButton'>Add item</button><button id='removeButton'>Remove item</button>
`
document.querySelector('.bottom-section').innerHTML = finalHtml;
addButton.addEventListener('click', () => {
addToItemList();
});
removeButton.addEventListener('click', () => {
removeLastItem();
});
listenToClicks("removeItem", function(index) {
removeFromSpecificIndex(index);
})
}
function listenToClicks(className, action) {
// Apply click handler to all elements with matching className
var allElements = document.body.getElementsByTagName("*");
for(var x = 0, len = allElements.length; x < len; x++) {
if(allElements[x].className == className) {
allElements[x].onclick = handleClick;
}
}
function handleClick() {
var parentElement = this.parentNode;
var childrenOfParent = parentElement.childNodes;
var index = 0;
for(var x = 0; x < childrenOfParent.length; x++) {
console.log(childrenOfParent[x]);
if(childrenOfParent[x] == this) {
break;
}
if(childrenOfParent[x].className == className) {
index++;
}
}
action.call(this,index);
}
}
updateHTML(items);
This method was inspired by the solution found in Get clicked class index javascript
The listenToClicks() function sets up an event listener on an HTML tag and provides the index of that tag relative to others with the same name.