lcdsantos / jQuery-Selectric

jQuery plugin for easy manipulation and customization of HTML selects
http://selectric.js.org/
MIT License
727 stars 157 forks source link

Copy attributes from original OPTION elements #228

Open vertigoelectric opened 6 years ago

vertigoelectric commented 6 years ago

My original OPTION elements have attributes that I need to interact with through the selectric dropdowns. In this particular case, I need to the "title" attribute in order to get some text to pop up when hovering over an option.

How can I get selectric to copy the attribute from the original OPTION elements to the selectric LI elements?

vertigoelectric commented 6 years ago

For now, this actually seems to do the job:

        $('select').each(function() {
            $('option').each(function() {
                var option = $(this);
                var title = option.attr("title");
                if(title)
                {
                    var name = option.text();

                    option.closest(".selectric-wrapper").find(".selectric-items").find("ul > li").each(function() {
                        if ($(this).text() == name)
                        {
                            $(this).attr("title",title);
                        }
                    });

                    //console.log(name + ":  " + title);                    
                }               
            });
        });