After incorporating a button that generates an unordered list from an array (e.g. var list_content = ["Apple", "Banana", "Orange", "Mango", "Papaya"];), each item of the array is displayed as a list item through the li element. The resulting list is appended to a designated div target.
var question4 = document.querySelector('#btn4');
question4.addEventListener('click', switch4);
var listContent = ['Apple', 'Banana', 'Orange', 'Mango', 'Papaya'];
function switch4() {
var newElement = document.createElement('Li');
div4.appendChild(newElement);
for (var i = 0; i < listContent.length; i++) {
newElement.textContent += listContent[i];
}
}
Upon clicking the button on the webpage, the string 'AppleBananaOrangeMangoPapaya' appears after every click.
When adjusting the for loop to read:
newElement.textContent = listContent[i];
Only 'Papaya' is then displayed.
The objective is for the button to display 'Apple', 'Banana', 'Orange', 'Mango', and 'Papaya' individually after each button press (such as 'Apple' on the first click, 'Banana' on the second click, and so on). Assistance is needed in achieving this desired functionality.