Icons in jQuery mobile device user selection menu

I am using a custom selection menu from jQuery Mobile, and I would like to put icons in a custom popup menu to accompany each one option. I apply an attribute data-iconto each option, for example:

<select name='mySelect' id='mySelect' data-icon='gear'>
    <option value='0' data-icon='star'>Option 0</option>
    <option value='1' data-icon='star'>Option 1</option>
    <option value='2' data-icon='star' selected="selected">Option 2</option>
</select>

FWIW, I already checked that my custom icons work in the select button itself. Am I just completely wrong in expecting the appearance of icons in the user menu?

+3
source share
1 answer

This is not supported by default, but here is a quick piece of code that allows:

//wait for the correct page to initialize
$(document).delegate('#home', 'pageinit', function () {

    //loop through each of the SELECT elements in this page
    $.each($(this).find('select'), function () {

        //get the ID of this select because it menu ID is based off of it
        var currentID = this.id;

        //iterate through each of the OPTION elements for this SELECT element
        $.each($(this).find('option'), function (index, element) {

            //if the OPTION element has the `data-icon` attribute
            if ($(element).attr('data-icon') != undefined) {

                //update the menu for this SELECT by adding an icon SPAN element
                //to each of the OPTION elements that has a `data-icon` attribute
                $('#' + currentID + '-menu').children().eq(index).find('.ui-btn-inner').append('<span class="ui-icon ui-icon-' + $(element).attr('data-icon') + ' ui-icon-shadow" />');
            }
        });
    });
});​​

Here is a demo: http://jsfiddle.net/NHQGD/

+4
source

All Articles