Mobius1/Selectr

Can't get the selectr instance after instantiated

Closed this issue · 2 comments

Hi,
I'm initializing all the selects in my page that contains a specific data attribute (e.g., data-context="selectr") on page load. However, I'm not storing the instances in variables, since there are several selects on that page. So, I need to update/enable/disable some selects and I didn't find a way to get the instance attached to the select component. By doing Selectr('#foo') I get an error and creating a new instance with new Selectr('#foo') will generate another selectr.
In Select2, for instance, if we call $el.select2('#foo') we will get the instance attached to that select, but I didn't find anything like this in selectr.

Is there a way to get the selectr instance attached to a select, or is this a bug? Or is it by design?

This is as simple as adding this.el.selectr = this; so you can access the selectr property on the select element when needed.

I'll add it for the next release.

As of v2.4.8, you can access the instance:

const el = document.getElementById("mySelect");

// create anonymous instance
new Selectr(el, config);

// get the instance
el.selectr