Ask Your Question

How can I output an array into an already existing list and span?

asked 2023-03-11 11:00:00 +0000

plato gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2022-09-25 08:00:00 +0000

bukephalos gravatar image

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">

// 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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer

Question Tools


Asked: 2023-03-11 11:00:00 +0000

Seen: 16 times

Last updated: Sep 25 '22