0

I have a piece of code:

let allItems = document.querySelectorAll(".cars-database-collection-item");

let allModels = $('.cars-tab-vehicle--model-span').map(function() {
  return this.textContent;
}).get();

which gives me:

allModels - ['A7 55 TFSIe PHEV', 'A9 e-tron', 'e-tron', 'e-tron GT', 'e-tron Sportback', 'Q4 e-tron quattro', 'Q4 Sportback e-tron', 'Q5 55 TFSIe PHEV', '330e', '530e']

allItems = NodeList(10) [div.cars-database-collection-item.w-dyn-item, div.cars-database-collection-item.w-dyn-item, div.cars-database-collection-item.w-dyn-item, div.cars-database-collection-item.w-dyn-item, div.cars-database-collection-item.w-dyn-item, div.cars-database-collection-item.w-dyn-item, div.cars-database-collection-item.w-dyn-item, div.cars-database-collection-item.w-dyn-item, div.cars-database-collection-item.w-dyn-item, div.cars-database-collection-item.w-dyn-item]

I want to replace all classes:

cars-database-collection-item.w-dyn-item

with (respectively) a names from an array.

What I tried:

window.addEventListener("load", () => {
allItems.forEach((n) => 
    function changeClass(value){
    class = allModels;
    allItems.className = class[value];
}
)   
})

Any kind of help appreciate.

Mr_Yoshi
  • 13
  • 6
  • Could you give an example of what you want to achieve? – Andy Apr 07 '22 at 14:25
  • Could you show some attempts to solve the problem? – nicael Apr 07 '22 at 14:25
  • You're using jQuery. `.removeClass()` and `.addClass()` are available to you. But like the Andy and nicael said, have you got any code showing how you've attempted to solve this yourself? – Pellay Apr 07 '22 at 14:29
  • 1
    Small aside: why are you mixing native JS and jQuery like that? – Andy Apr 07 '22 at 14:29
  • @Mr_Yoshi add it to your question and explain what you tried. Might wanna have a look at this too: https://stackoverflow.com/help/how-to-ask. This is the reason you're getting downvotes. – Pellay Apr 07 '22 at 14:31
  • Does this answer your question? [How to add a class to a given element?](https://stackoverflow.com/questions/507138/how-to-add-a-class-to-a-given-element) – Mark Baijens Apr 07 '22 at 14:40

0 Answers0