@@ -67,7 +67,11 @@ return $.widget( "ui.selectmenu", {
6767 } ,
6868
6969 _drawButton : function ( ) {
70- var that = this ;
70+ var that = this ,
71+ item = this . _parseOption (
72+ this . element . find ( "option:selected" ) ,
73+ this . element [ 0 ] . selectedIndex
74+ ) ;
7175
7276 // Associate existing label with the new button
7377 this . label = $ ( "label[for='" + this . ids . element + "']" ) . attr ( "for" , this . ids . button ) ;
@@ -99,12 +103,9 @@ return $.widget( "ui.selectmenu", {
99103 } )
100104 . prependTo ( this . button ) ;
101105
102- this . buttonText = $ ( "<span>" , {
103- "class" : "ui-selectmenu-text"
104- } )
106+ this . buttonItem = this . _renderButtonItem ( item )
105107 . appendTo ( this . button ) ;
106108
107- this . _setText ( this . buttonText , this . element . find ( "option:selected" ) . text ( ) ) ;
108109 this . _resizeButton ( ) ;
109110
110111 this . _on ( this . button , this . _buttonEvents ) ;
@@ -190,7 +191,11 @@ return $.widget( "ui.selectmenu", {
190191
191192 refresh : function ( ) {
192193 this . _refreshMenu ( ) ;
193- this . _setText ( this . buttonText , this . _getSelectedItem ( ) . text ( ) ) ;
194+ this . buttonItem . replaceWith (
195+ this . buttonItem = this . _renderButtonItem (
196+ this . _getSelectedItem ( ) . data ( "ui-selectmenu-item" )
197+ )
198+ ) ;
194199 if ( ! this . options . width ) {
195200 this . _resizeButton ( ) ;
196201 }
@@ -275,6 +280,15 @@ return $.widget( "ui.selectmenu", {
275280 return this . menu ;
276281 } ,
277282
283+ _renderButtonItem : function ( item ) {
284+ var buttonItem = $ ( "<span>" , {
285+ "class" : "ui-selectmenu-text"
286+ } ) ;
287+ this . _setText ( buttonItem , item . label ) ;
288+
289+ return buttonItem ;
290+ } ,
291+
278292 _renderMenu : function ( ul , items ) {
279293 var that = this ,
280294 currentOptgroup = "" ;
@@ -480,7 +494,7 @@ return $.widget( "ui.selectmenu", {
480494
481495 // Change native select element
482496 this . element [ 0 ] . selectedIndex = item . index ;
483- this . _setText ( this . buttonText , item . label ) ;
497+ this . buttonItem . replaceWith ( this . buttonItem = this . _renderButtonItem ( item ) ) ;
484498 this . _setAria ( item ) ;
485499 this . _trigger ( "select" , event , { item : item } ) ;
486500
@@ -590,22 +604,27 @@ return $.widget( "ui.selectmenu", {
590604 } ,
591605
592606 _parseOptions : function ( options ) {
593- var data = [ ] ;
607+ var that = this ,
608+ data = [ ] ;
594609 options . each ( function ( index , item ) {
595- var option = $ ( item ) ,
596- optgroup = option . parent ( "optgroup" ) ;
597- data . push ( {
598- element : option ,
599- index : index ,
600- value : option . attr ( "value" ) ,
601- label : option . text ( ) ,
602- optgroup : optgroup . attr ( "label" ) || "" ,
603- disabled : optgroup . prop ( "disabled" ) || option . prop ( "disabled" )
604- } ) ;
610+ data . push ( that . _parseOption ( $ ( item ) , index ) ) ;
605611 } ) ;
606612 this . items = data ;
607613 } ,
608614
615+ _parseOption : function ( option , index ) {
616+ var optgroup = option . parent ( "optgroup" ) ;
617+
618+ return {
619+ element : option ,
620+ index : index ,
621+ value : option . attr ( "value" ) ,
622+ label : option . text ( ) ,
623+ optgroup : optgroup . attr ( "label" ) || "" ,
624+ disabled : optgroup . prop ( "disabled" ) || option . prop ( "disabled" )
625+ } ;
626+ } ,
627+
609628 _destroy : function ( ) {
610629 this . menuWrap . remove ( ) ;
611630 this . button . remove ( ) ;
0 commit comments