0

I have a page that loads a HTMLCollection that consists of 1000 elements.

What I want to achieve is once the page is loaded, immediately after, I'd like that HTMLCollection to be shuffled. (I have no control on how I receive the data from the server)

I can't figure out the best way to do this. Should I copy the HTMLCollection into another temp variable, remove all items in the existing HTMLCollection (on the DOM), shuffle that temp and display it - in which case how does one remove HTMLCollection items?

Or is it possible to manipulate the HTMLCollection and DOM as it is - in which case how is that done?

I'd gather the HTML collection using this:

let allImageTiles = document.getElementsByClassName('locationImage');

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
userMod2
  • 8,312
  • 13
  • 63
  • 115

1 Answers1

1

Should I copy the HTMLCollection into another temp variable, remove all items in the existing HTMLCollection (on the DOM), shuffle that temp and display it ...

Yes, definetly. Every operation on the DOM directly is heavy, as it has to be reflected immeadiately to the things the user sees, or in other words: The page has to be rerendered, and you don't want to do that more often than necessary.

in which case how does one remove HTMLCollection items?

There is ChildNode.remove(). However you could also just append to the elements at another position in the tree, that will remove them from their previous position automatically.

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151