var list = [{
name: "koala",
owner: "mark"
},
{
name: "opossum",
owner: "lala"
},
{
name: "dog",
owner: "lucia"
}
];
var str = document.body.innerHTML;
var toChange = /fox|penguin|cat/ig;
for (let listItem of list) {
str = str.replace(toChange, listItem.name);
str.id = 'span';
document.body.innerHTML = str;
console.log(listItem.name);
}
span {
text-decoration: underline;
color: red;
}
The fox jumps over the bush, the penguin walks, the cat drinks.
What I basically want to do is to replace the animals in the HTML text with the animals in "list". Additionally I want these last changed animal names to change color as well. I managed to replace with the animal Koala, but I am not really sure why it only replaces with Koala, as if I console.log(listItem.name) I get all three animals. Is there a problem with the for loop?