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

use data-custom-properties #982

Closed
reimax opened this issue Dec 21, 2021 · 3 comments · Fixed by #1001
Closed

use data-custom-properties #982

reimax opened this issue Dec 21, 2021 · 3 comments · Fixed by #1001

Comments

@reimax
Copy link

reimax commented Dec 21, 2021

hi, i have html select

<select>
  <option value="Test1" data-custom-properties="test">Test1</option>
  <option value="Test2" data-custom-properties="test">Test2</option>
  <option value="Test3" data-custom-properties="test">Test3</option>
</select>

i try get data-custom-properties after change

document.querySelectorAll("select[name=operation]").forEach(function(element) { 
        element.addEventListener("change", (event) => {
            var show_droplist = event.target.selectedOptions[0].attributes;
            console.log(show_droplist);
            
        });       
    });

and show_droplist have data-custom-properties:
value: "[object Object]"

how get data attr from change options?

@reimax reimax added the bug label Dec 21, 2021
@reimax reimax changed the title <select> <option value="Test1" data-custom-properties="testloremtext">Test1</option> <option value="Test2" data-custom-properties="test lorem text">Test2</option> <option value="Test3" data-custom-properties="{'description': 'item description'}">Test3</option> </select> use data-custom-properties Dec 21, 2021
@mtriff
Copy link
Member

mtriff commented Dec 21, 2021

I can duplicate this issue. It looks like it is a regression and will need to be fixed.

Also, you may want to use events provided by Choices instead of adding an event listener directly to the select elements. Such as:

<select  id="choicesTest">
  <option value="Test1" data-custom-properties="test">Test1</option>
  <option value="Test2" data-custom-properties="test">Test2</option>
  <option value="Test3" data-custom-properties="test">Test3</option>
</select>
const choicesTest = new Choices(document.getElementById('choicesTest'));
choicesTest.passedElement.element.addEventListener('change', (event) => {
  console.log(event.currentTarget.selectedOptions[0].dataset.customProperties);
});

@reimax
Copy link
Author

reimax commented Dec 21, 2021

in code:

<select class="choices__input" name="test" hidden="" tabindex="-1" data-choice="active">
<option value="2" **data-custom-properties="[object Object]"**>test</option>
</select>

from original html code properties not passed.

i need get properties stanalone. in my code i init Choices.js to all select on project. and in same time i need on page get properties on change.
that's why I'm trying to find how to pass the parameter to .choices__inner block

@reimax
Copy link
Author

reimax commented Dec 22, 2021

if after init choices i use .desctroy() - all data-custom-properties lost on select element

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants