I think I somehow messed up on the way and made things more complicated than it actually is, so here it goes... So if anyone got a hint on how I should do instead I would be glad.
At first I got an array
const cars = ["Saab", "Volvo", "BMW"];
My problem here was that I wanted an a tag inside every array value, which I found out wasn't so easy. So i did like this:
const cool = '<a href="coolLink?id=1">Cool link</a>';
And here comes my loop:
cars.forEach(function(e, index){
$('.row')[index].append(e);
$('.row')[index].insertAdjacentHTML('beforeend' , cool);
});
So I was able to make it work, my cool links works as intended. However as you might see in my href I currently got an id there. All cars should have an unique id. Is there anyway I can 1 the id everytime it gets looped? Or can I acheive this in another way? Like this which doesn't work:
const cars = ["Saab<a href="coolLink?id=1">Cool link</a>", "Volvo<a href="coolLink?id=2">Cool link</a>", "BMW<a href="coolLink?id=3">Cool link</a>"];
CodePudding user response:
If you define cool
inside the "loop" then you can make use of the index
when defining it, for example:
cars.forEach(function(e, index){
const cool = `<a href="coolLink?id=${index 1}">Cool link</a>`;
$('.row')[index].append(e);
$('.row')[index].insertAdjacentHTML('beforeend' , cool);
});