Search code examples
javascriptprototypejshtml-select

Find out if the number of selected items > 1 in a multiple select box using prototype


Is there a way I can find out if there are more than one items selected in a multiple select box?

I know it can be done in a linear fashion by going through each option individually, but I would like to avoid that because I am just going to enable or disable a UI component based on the number of items selected.

As this operation does not really require the values nor text value of the selected items, I am looking for a simple way like using pluck or something similar. Any help would be greatly appreciated.


Solution

  • In jQuery you can use the descriptor;

    $('.select-box:selected')
    

    to return a list of all the selected items in a multiple select box. You can then use .size() to tell you how many there are.

    if ( $('.select-box:selected').size() > 1 ) {
       // do stuff
    }