FMM
FMM

Reputation: 4329

Icons in Custom jQuery Mobile Select Menu

I'm using a custom select menu from jQuery Mobile, and I'd like to put icons into the custom pop-up menu to accompany each option. I'm applying the data-icon attribute to each option, like so:

<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've already verified that my custom icons work in the select button itself. Am I just completely wrong in expecting icons to appear in the custom menu?

Upvotes: 3

Views: 5519

Answers (1)

Jasper
Jasper

Reputation: 75993

This is not supported by default but here is a quick piece of code to make it possible:

//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's menu's 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/

Upvotes: 4

Related Questions