selectize.js icon indicating copy to clipboard operation
selectize.js copied to clipboard

Unable to query original select

Open marcioaguiar opened this issue 13 years ago • 7 comments

When you select an option with selectize it changes the original select component adding an option with the selected value

for (i = 0, n = this.items.length; i < n; i++) {
                options.push('<option value="' + htmlEntities(this.items[i]) + '" selected="selected"></option>');
            }

I think the option component should reflect exactly how the original option was (including the label) so the user can query the select component and get the label and wherever data he wants from it.

marcioaguiar avatar Jun 20 '13 15:06 marcioaguiar

The reasoning behind not keeping the DOM up to date with the non-selected options had to do with performance. In some cases, it could lead to managing thousands of <option> tags—which could add some sluggishness without much advantage added. Furthermore, it complicates the code / makes the library larger. Could you provide a use case that shows how reading options from the DOM after-the-fact is useful?

brianreavis avatar Jun 24 '13 05:06 brianreavis

@brianreavis since I've not had time to have a look at it, I assume there is no reason why the plugin architecture couldn't handle this?

jbrooksuk avatar Jun 24 '13 08:06 jbrooksuk

I agree that it's not necessary to add all options to the selected element. Only the selected one is fine, but my problem is that even the selected option does not fully reflect the dom of the original option. It just creates an option element with the same value as the original option.

In my use case I needed to have access to the LABEL of the selected option. I think that will not affect performance or code readability. Since it's a minor thing I'll try to implement myself and submit a pull request when I have time.

Thanks :)

marcioaguiar avatar Jun 25 '13 14:06 marcioaguiar

@brianreavis

Could you provide a use case that shows how reading options from the DOM after-the-fact is useful?

My problem: I have added data-* attributes in option tags and using these attributes for calculate different things in JS later. But I can't use them because selectize.js remove these attributes. So, I think selectize.js should respect data-* attributes in options. Also, I think this is will be appropriate solution for @marcioaguiar problem.

Could you please support data-* attributes in option tags in next selectize.js version?

Thanks

maxd avatar Sep 08 '13 19:09 maxd

Could you provide a use case that shows how reading options from the DOM after-the-fact is useful?

@brianreavis I need to display a live preview of the options selected. For this I need the original innerText of the option. From what I can tell, the selectize callbacks (specifically onChange) only returns value. In my case the value of the option is just an id.

amccloud avatar Jan 22 '14 21:01 amccloud

+1 for this issue. dup: https://github.com/brianreavis/selectize.js/issues/598

@brianreavis We're currently using select2 and attempting to move selectize breaks our integration test since they interact with elements in the dom.

desheikh avatar Nov 01 '14 16:11 desheikh

closing stale issues older than one year. If this issue was closed in error please message the maintainers. All issues must include a proper title, description, and examples.

risadams avatar Nov 11 '20 17:11 risadams