0

The question is quite simple, I'm wondering how I can create HTMLCollection. Let's say I have some divs inside var. I'm wondering how I can create HTML collection from that variable. So far it doesn't let me interact with it.

var h = new HTMLCollection(somevar);

I tried doing it with a constructor but it doesn't seem to have a constructor. I even tried with simple = operator but no luck. Is there a quick and clean solution?

I've seen some solutions posted here like: create a HTMLCollection and I know I can't add to the collection as said here: How to add an element into the HTMLCollection in javascript?

In my case, I'm not trying to add to the collection but create a new one from a pre-existing variable.

EDIT:

My example:

var triplesingleslides = $(v)[0];

I'd like this triplesingleslides to be HTMLCollection. Inside triplesingleslides there are 3 div elements.

Why it's not a duplicate question:

It's asking if there is a simpler way to do it than those that already have complex answers and it would seem that there isn't, so this question is answered.

sesmajster
  • 752
  • 7
  • 32
  • 1
    What's the use case? I can't think of anything that needs an HTMLCollection object in particular. – Quentin Jan 14 '19 at 11:43
  • Indeed. What can a HTMLCollection do that a basic DOM tree can't? – Mr Lister Jan 14 '19 at 11:44
  • 1
    "I've seen some solutions posted here like: create a HTMLCollection" — So what is wrong with those solution? – Quentin Jan 14 '19 at 11:44
  • Well, it's not so much about for what can it be used as it is the question of possibility as in, is it possible and if yes, then how? For the pure example I was trying to do something like: `slides[slideIndex-1].style.display = "block";` – sesmajster Jan 14 '19 at 11:49
  • "Let's say I have some divs inside var." - what do you mean? How is this var defined? – markmoxx Jan 14 '19 at 11:50
  • 1
    @user3029612 You can do that with an ordinary array. If the nodes is in a DOM tree, then you could use [querySelector](https://developer.mozilla.org/en-US/docs/Web/API/Document/querySelector) to get a collection. – Emil S. Jørgensen Jan 14 '19 at 11:51
  • 1
    "it's not so much about for what can it be used" — Yes it is! [Take the tour](https://stackoverflow.com/tour). *Focus on questions about an **actual problem** you have faced.* Hypothetical questions where an answer won't solve any real problems aren't a good fit for SO. – Quentin Jan 14 '19 at 11:57
  • [There was a question](https://stackoverflow.com/questions/38440492/how-can-i-convert-an-array-of-nodes-to-a-static-nodelist/38446348#38446348) about this, it appears to be quite complex, but more or less possible. – Teemu Jan 14 '19 at 11:57
  • Yea I saw the complex solutions, sure there has to a better way, no? – sesmajster Jan 14 '19 at 11:59
  • `HTMLCollection` is an interface for a Web API that's almost always implemented in the core JavaScript engine (written in C or whatever). There's little reason to have a better way and I'm surprised a complex workaround exists at all. – Álvaro González Jan 14 '19 at 12:05
  • I'm afraid there's no simple way. – Teemu Jan 14 '19 at 12:05

0 Answers0