I'm working with an ecommerce page that I have no control over the HTML. I'm trying to add a price to every list item (swatch).
// This is some hidden HTML being displayed on the page that has altered prices in it.
const superAttributesRaw = [...document.querySelectorAll('.super-attribute-select option')];
const superAttributes = superAttributesRaw.filter(newValue => Object.keys(newValue).length !== 0);
// This is the shown HTML swatch I need to add the altered prices to.
const swatchUl = [...document.querySelectorAll('.swatchesContainer ul li')];
// Pulling out the altered prices and putting them into an array.
prices = [];
for (let value of superAttributes) {
prices.push(value.config.price);
}
// This adds the sample text where I need it in the unordered list of swatches. I just need 'test' to be the altered prices.
for (let value of swatchUl) {
value.insertAdjacentHTML('beforeend', '<span>' + 'test' + '</span>');
}
My prices array looks like:
prices = ["0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","0","-2","0","0","0","0","0","0","0"]
Where the "test" is in the 2nd for loop, I need that to somehow display the prices. I'm unsure how to have it iterate over every item in the price loop and display the 1st, 2nd, 3rd, etc.. in the correct order.
I tried nesting the for loops and it got all wonky. My terminology is pretty terrible if it wasn't obvious, so I'm having a hard time googling for exactly what I need. Any help is appreciated. Thanks!