Create a jQueryUI 1.8 Button menu

Brian M. Hunt picture Brian M. Hunt · Apr 14, 2010 · Viewed 29.4k times · Source

Now that jQueryUI 1.8 is out, I was browsing through the updates and came across the new Button widget, and in particular one of the demos with the SplitButton with a dropdown. This demo seems to suggest that the Button widget could be used to create a dropdown menu here.

As a matter of discussion, I was wondering what ways there are to go about creating a dropdown menu with this new Button widget.

Cheers.

Answer

Robert Harvey picture Robert Harvey · Apr 14, 2010

You would have to drop a list below the button, in a manner similar to the demo provided here for autocomplete: http://jqueryui.com/demos/autocomplete/.

Essentially, you would replace the code in the button demo that displays the alert "could display a menu with a selected action" with code that does just that. This code can fire off one of the many jQuery Menu plugins out there, like this one.

<div class="demo">

    <div>
        <button id="rerun">Run last action</button>
        <button id="select">Select an action</button>
    </div>

</div>

<script type="text/javascript">
$(function() {
    $("#rerun").button().click(function() {
        alert("Running the last action");
    })
    .next()
    .button({
        text: false,
        icons: {
            primary: "ui-icon-triangle-1-s"
        }
    })
    .click(function() {
        // Code to display menu goes here. <<<<<<<<<<<<
    })
    .parent()
    .buttonset();
});