You can output an array into an already existing list and span by using JavaScript DOM manipulation. Here is an example:
// HTML code
<ul id="myList">
<li><span></span></li>
<li><span></span></li>
<li><span></span></li>
</ul>
// JavaScript code
let array = ["item1", "item2", "item3"];
let list = document.getElementById("myList");
let items = list.getElementsByTagName("li");
for (let i = 0; i < items.length; i++) {
let span = items[i].getElementsByTagName("span")[0];
if (array[i]) {
span.textContent = array[i];
}
}
In this example, we first define an array called "array" with three items. Then, we use JavaScript to get the list element with the id "myList" and the li elements inside it. We loop through each li element and get the first span element inside it. If the current index of the array exists, we set the text content of the span element to the corresponding item in the array. The end result is that the array values are outputted into the spans of the already existing list.
Asked: 2023-03-11 11:00:00 +0000
Seen: 19 times
Last updated: Sep 25 '22