Skip to content
This repository was archived by the owner on Apr 18, 2026. It is now read-only.
This repository was archived by the owner on Apr 18, 2026. It is now read-only.

Preserve custom HTML5 data attributes #239

Closed

Description

Feature request

Preserve custom HTML5 data attributes, so that in this case data-fruit won't disappear when going from <select> to Selectize:

http://jsfiddle.net/k44p2by6/

Activity

  1. MaherSaif commented on Jan 13, 2014

    @MaherSaif

    +1

  2. MaherSaif commented on Jan 13, 2014

    @MaherSaif

    I've created a plugin to override -not extend- the function which changes the original select options.

    Selectize.define('preserve_original_select', function(options) {
      var self = this;
    
      this.updateOriginalInput = (function() {
        var original = self.updateOriginalInput;
        return function() {
          var i, n, options, self = this;
    
          if (self.$input[0].tagName.toLowerCase() === 'select') {
            for (i = 0, n = self.items.length; i < n; i++) {
              self.$input.find('[value="'+self.items[i]+'"]').prop('selected', true);
            }
          } else {
            self.$input.val(self.getValue());
          }
    
          if (self.isSetup) {
            self.trigger('change', self.$input.val());
          }
    
          // original.apply(this, arguments);
        };
      })();
    
    });

    Don't forget to load the plugin.

  3. xak2000 commented on Mar 19, 2014

    @xak2000

    Plugin is good but data-attributes in OPTIONs shouldn't disappear by default.
    It is hurt for transparent selectizing of existing SELECT elements.

  4. dylanbromby commented on Mar 31, 2014

    @dylanbromby

    Yes! Selectize should be updated to allow for the preservation of custom attributes in each option.

  5. vizcay commented on May 17, 2014

    @vizcay

    +1

  6. ryannealmes commented on Jun 25, 2014

    @ryannealmes

    Would really appreciate a fix to this. Can't get plugin working.

  7. jcomack commented on Aug 25, 2014

    @jcomack

    +1

  8. jontjs commented on Aug 27, 2014

    @jontjs

    Agreed, selectize should retain data attributes

  9. nexenator commented on Sep 1, 2014

    @nexenator

    +1

  10. dylanbromby commented on Sep 1, 2014

    @dylanbromby

    I have no idea how I got CC'd on all the posts to this list, but I'd appreciate it if it stopped. Thanks!

    On Sep 1, 2014, at 8:05 AM, Stephan Helbig notifications@github.com wrote:

    +1

    —
    Reply to this email directly or view it on GitHub.

  11. arjan0307 commented on Oct 20, 2014

    @arjan0307

    +1

  12. jensljungblad commented on Dec 17, 2014

    @jensljungblad

    +1

  13. telmogh commented on Dec 17, 2014

    @telmogh

    +1

  14. iamroi commented on Dec 21, 2014

    @iamroi

    +1

  15. iamroi commented on Dec 21, 2014

    @iamroi

    This is working.

    $(ele).selectize({
    .....
    dataAttr: 'data-extra', //default is data-data
    .....
    }
    });

    Add data-extra in every option as json encoded, Ex
    option value="one" data-extra="{'test':'123'}"
    option value="two" data-extra="{'test':'456'}"

    Get the selectize instance using this, var selectize = $(ele)[0].selectize

    Users selected options can be found here: selectize.items

    Data attributes of all options can be found here: selectize.options

    In our case it will be,
    selectize.options['one'].test
    selectize.options['two'].test

    Let me know if this is working.

  16. 39 remaining items

  17. smohadjer commented on Sep 8, 2018

    @smohadjer

    @KazutoDE Here is a working example using solution I suggested earlier. Note that the data attribute (in this case data-continent) would be gone from original option element after selectize is initialized, but it will be preserved on markup generated by selectize, so you can still access it.

    http://sandbox.saeidmohadjer.com/selectize/

  18. RudeySH commented on Dec 15, 2018

    @RudeySH

    #1058 by @dakuzen highlights an important issue when using the $.extend workaround by @indrimuska.

  19. naveed-ahmad commented on Jan 9, 2019

    @naveed-ahmad

    @KazutoDE @WadeTheFade

    Sample code which worked for me.

    HTML markup

    <select>
    <option data="{value:1, name: 'data 1'}" value="3"> First option</option>
    <option data="{value:2, name: 'data 2'}" value="3"> Second option</option>
    </select>
    

    Init the selectize:

        $("select").selectize({dataAttr: 'data'})
    

    Then you can fetch the data attributes using:

    $("select").data().selectize.options['1'].name  => data 1
    $("select").data().selectize.options['2'].name  => data 2
    

    No need to overrider the selectize's renderer or anything.

  20. Younnah commented on Feb 21, 2019

    @Younnah

    Hi @naveed-ahmad !
    I tried your code, but it does not work at all for me...

    When I write
    $("select").data().selectize.options['1'].name
    The result is "undefined"

    To hope to have an exit, I have to write
    $("select").data().selectize.options["3"]
    and the result is
    {text: "First", value: "3", $order: 1}

    I have only this option in my options array...
    I tested many things but nothing that works to recover the data...

    Thanks for your help !

  21. naveed-ahmad commented on Feb 21, 2019

    @naveed-ahmad

    @Younnah please see data attribute in my html, data="{value:1, name: 'data 1'}" i've name and value keys.

    In your case keys are text, value, $order
    so $("select").data().selectize.options['1'].name SHOULD be undefined.

    Try $("select").data().selectize.options['1'].text and value should be First

  22. Younnah commented on Feb 22, 2019

    @Younnah

    Yes, I see, but I can not access the attribute "name" in the "data" ...

  23. olucasu commented on Jun 14, 2019

    @olucasu

    The way that worked for me

    My only goal was to get the "selected" data-attr from the select

    Get all data-attr
    thanks to @indrimuska

    $('.js-select').selectize({
            onInitialize: function () {
                var s = this;
                s.revertSettings.$children.each(function () {
                    $.extend(s.options[this.value], $(this).data());
                });
            }
        });
    

    then inside the eventListener:

    $('.js-select').change((e) => { 
        const value = $(e.currentTarget).val(); 
        const options = $(e.currentTarget).data().selectize.options;
       // There you have it
       return options[value];
    })
    
    
    
  24. risadams commented on Dec 1, 2020

    @risadams
    Contributor

    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.

  25. smohadjer commented on Dec 1, 2020

    @smohadjer

    @risadams I would have liked to see this shortcoming of Selectize to be fixed properly.

  26. databyte commented on Dec 1, 2020

    @databyte
    Collaborator

    It's a fair request. We're currently focused on getting the project actively maintained with security patches, dependency updates, processing the 120 opened PRs, etc.

    This is a very valid enhancement and PRs are welcomed.

    If anyone has bandwidth to work on this, open up a new PR against this issue and we can re-open it.

  27. databyte commented on Dec 1, 2020

    @databyte
    Collaborator

    Looks like maybe #1194 addresses this too. We need to process all the PRs including their functionality, test coverage and all the usual suspects including performance. If you want to pull that PR down and check it too, the more the merrier!

  28. danieleklekoo commented on Jun 6, 2022

    @danieleklekoo

    selectize.txt
    In attachment my implementation.
    In order to see the changes search for the word "attributes".

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions