Search code examples
javascriptjqueryjquery-uiselectablejquery-ui-selectable

JQuery UI Selectable(): Check if only one element is selected


How can I check if only one element was selected when using JQuery UI's selectable()? So if I just click on one element and select it, an alert box would show. But if I used the lasso to select multiple items, the alert box would not show.

$('#area').selectable({
    selected: function (event, ui)
    {
        //if number of selected elements=1:
        //do something

    }
);

How would I do this?


Solution

  • Use the length property to check the number of items selected:

    $('#area').selectable({
        selected: function (event, ui) {
            if ($('.ui-selected').length === 1) {
                 //only one selected
            }    
        }
    );