Skip to content
Advertisement

Does element.replaceChildren() function work with duplicate values, or what is stopping this from working

currently i am trying to replace an elements children with the same value in an array
e.g:

JavaScript

my code is this:

JavaScript

currently the fill function works fine and as intended
the mock element class also works fine

Advertisement

Answer

JavaScript objects are references. This means that there is only a single h.element in the memory of your whole program, so if you tell the DOM to replace children with h.element 5 times, it will only insert it once because it is five references to a single element.

You must create multiple elements.

With your code example, that would look like this:

JavaScript

Check out this article to learn more: https://dmitripavlutin.com/value-vs-reference-javascript/

Advertisement