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

Passing attributes from html data #182

Closed
kjugi opened this issue Jun 14, 2017 · 5 comments
Closed

Passing attributes from html data #182

kjugi opened this issue Jun 14, 2017 · 5 comments

Comments

@kjugi
Copy link

kjugi commented Jun 14, 2017

Is there any support for data attributes from HTML? I want to pass the values on specific select item and only to it. Can't do it in JS by passing id or other attribute b/c it must me reusable element. And to all, I'm passing default values or same classes too. Can I in another way pass element 2 times or in 2 different ways?

The best way is to pass data attributes to select and when calling Choices with default values add this custom one if existing and pass as parameters too.

If want some code can prepare something or show concept

@jshjohnson
Copy link
Collaborator

You can't currently add custom properties via HTML but as of version 2.8.6, you can pass custom properties to items/choices via JS . Will re-open this if more people request this.

@sergonius
Copy link

sergonius commented Aug 2, 2017

It would be nice if the plugin allowed passing additional classes at least.

<select class="c-select c-select--dark">....</select>

after initialised with .c-select (new Choices('.c-select')), it will lose c-select--dark class.

Edit: ...and actually extending it to at least some other basic/popular functionality like placeholders and maybe some other options would rock.

Edit 2: I guess sending other params via a data attribute yourself is simple enough, but passing classes would still be useful for different variations/skins.

My solution for placeholders:

<select
  class="c-select"
  data-select-options='{"placeholderValue": "Select payment method"}'>
  ...
</select>
const selectWithOpts = document.querySelector('.c-select[data-select-options]');
const selectOptions = selectWithOpts && selectWithOpts.getAttribute('data-select-options');

new Choices(
  '.c-select[data-select-options]',
  JSON.parse(selectOptions),
);

@ChristopherRobinSuperStar
Copy link

ChristopherRobinSuperStar commented Nov 20, 2017

It is very necessary and convenient to pass data-attributes. Please, add it.

@ericmuijs
Copy link

ericmuijs commented Jun 19, 2022

@jshjohnson Can this one be reopened? It would like to have HTML data attribute support.

@valtermedeiros
Copy link

This is a must, especially if you are working with Livewire, PHP and Alpine.JS. It's a lot cleaner, I might push to this repo.

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

No branches or pull requests

6 participants