Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Select doesn't preserve optgroups after destroying Choices instance #1055

Closed
beyer96 opened this issue Sep 13, 2022 · 1 comment
Closed

Select doesn't preserve optgroups after destroying Choices instance #1055

beyer96 opened this issue Sep 13, 2022 · 1 comment
Labels

Comments

@beyer96
Copy link

beyer96 commented Sep 13, 2022

Describe the bug
I use Turbolinks in my app, so I need to destroy Choices instances everytime before Turbolinks caches the page. But when I destroy Choices instance on select with optgroups in it, these optgroups disappear in default select after destroying Choices.

JSFiddle here: https://jsfiddle.net/rjqzsum6/

I have workaround for this, in case someone else runs into this problem too. It replaces innerHTML of default select completely (but correctly, with optgroups):

const destroyChoices = (choices, select) => {
  const newOptgroupArray = [];

  select.innerHTML = choices._presetGroups.forEach(optgroup => {
    const newOptgroup = document.createElement("optgroup");
    newOptgroup.label = optgroup.label;
    newOptgroup.innerHTML = optgroup.innerHTML;
    newOptgroupArray.push(newOptgroup);
  });

  choices.destroy();
  select.innerHTML = "";
  newOptgroupArray.forEach(newOptgroup => {
    select.appendChild(newOptgroup);
  });
};

But it would be really nice not to use this 'hotfix'

@Xon
Copy link
Collaborator

Xon commented Aug 22, 2024

Implemented as part of #1166

@Xon Xon closed this as completed Aug 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants