l14045
l14045

Reputation: 63

JS DOM NODES: Javascript

With the help of a loop, I want to add an element into my list items.

Thanks a bunch for any advice!

Upvotes: 0

Views: 44

Answers (1)

Entest89
Entest89

Reputation: 87

It's a little hard to tell the context of your problem, but I think using a for...of loop would better meet your needs, as it works with each item of the list individually.

Try this:

let item = document.getElementsByTagName('li');

for (let i of item){
    i.innerHTML = `${i.innerText} <span class="credits">100 credits</span>`;
}

If you need something that specifically uses an index loop, try this:

let item = document.getElementsByTagName('li');

for (let i = 0; i < item.length; i++){
    item[i].innerHTML = `${item[i].innerText} <span class="credits">100 credits</span>`;
}

Upvotes: 2

Related Questions