Search code examples
jqueryjquery-ui

JQuery ui selectmenu placeholder is not working


I had added Jquery ui selectmenu placeholder is not working showing error

$.widget( 'app.selectmenu', $.ui.selectmenu, {
    _drawButton: function() {
        this._super();
        var selected = this.element
                .find( '[selected]' )
                .length,
            placeholder = this.options.placeholder;

        if (!selected && placeholder) {
            this.buttonText.text( placeholder );    
        }
    }
});

$('select').selectmenu({
    placeholder: 'Select a speed'
});

Error is text is not defined. I am using Jquery UI version 1.12.0


Solution

  • Instead of:

    this.buttonText.text( placeholder );  
    

    try with:

    this.buttonItem.text(placeholder);
    

    buttonText does exist: you have only button and buttonItem and you need buttonItem to change the text.

    Example:

    $.widget( 'app.selectmenu', $.ui.selectmenu, {
      _drawButton: function() {
        this._super();
        var selected = this.element
        .find( '[selected]' )
        .length,
            placeholder = this.options.placeholder;
    
        if (!selected && placeholder) {
          this.buttonItem.text(placeholder);
        }
      }
    });
    $(function () {
      $('#speed').selectmenu({ placeholder: 'Select a speed' });
    });
    <link href="https://code.jquery.com/ui/1.12.0/themes/smoothness/jquery-ui.css" rel="stylesheet"/>
    <script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
    <script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script>
    
    <label for="speed">Select a speed</label>
    <select name="speed" id="speed">
        <option>Slower</option>
        <option>Slow</option>
        <option>Medium</option>
        <option>Fast</option>
        <option>Faster</option>
    </select>