0

I'm pretty new to javascript and trying some things to learn. I'm however getting one issue and can't seem to figure it out. If I'm looking trough the nodelist with document.querySelectorAll(".product .actions .stock.outofstock") I get 10 results back:

enter image description here

I want to replace the inside from all these elements to something new.

I tried using the following:

let outofstock = document.querySelectorAll(".product.actions.stock.outofstock").innerHTML;
document.querySelectorAll(".product.actions.stock.outofstock").innerHTML= outofstock.replace(/Out of stock/g,'<i class="icon-negative"></i>Sold out');

That didnt work and gives me the following error:

enter image description here

however when I use the following code it works, but for only the first element:

let outofstock = document.querySelector(".product.actions.stock.outofstock").innerHTML;
document.querySelector(".product.actions.stock.outofstock").innerHTML= outofstock.replace(/Out of stock/g,'<i class="icon-negative"></i>Sold out');
  • use replaceAll property of javascript. – Shilpe Saxena Dec 05 '22 at 11:35
  • or iterate thru the array you are getting back. –  Dec 05 '22 at 11:35
  • @ShilpeSaxena that doesnt work either. It gives back the same error but then 'Cannot read properties of undefined (reading 'replaceAll') – Nickinatorz Dec 05 '22 at 11:46
  • @E.Maggini Do you have an example for that? – Nickinatorz Dec 05 '22 at 11:47
  • `const elements = document.querySelectorAll(".product.actions.stock.outofstock"); for (let i = 0; i < elements.length; i++) { /* do stuff with elements[i] */ }` or even `for (const element of elements) { /* do stuff with element */ }` – VLAZ Dec 05 '22 at 12:08
  • or map....or filter....or forEach.... really....Google is such a powerful tool..... –  Dec 05 '22 at 12:31
  • @VLAZ why close it only to (kinda) answer it in the comments? :-D –  Dec 05 '22 at 12:32
  • @E.Maggini Google is really powerfull, but no clue on how to google this specific issue. Been doing that for the past 2 hours with no results haha – Nickinatorz Dec 05 '22 at 12:34
  • @E.Maggini because OP asked in the comments. And also put the question up for reopening. This is an attempt to notify reopen reviewers that the duplicate closure is correct. – VLAZ Dec 05 '22 at 12:34
  • @Nickinatorz try to do it in console area console.log(yourVariableName.replaceAll(val, chnageVal)) – Shilpe Saxena Dec 06 '22 at 02:59

0 Answers0