user405398
user405398

Reputation:

How to get number of selected options using jquery?

I have a multiple selection list which have more than 5 options. But i want to restrict the selection of the options to 2 or 3 options. How to do it using jquery? How to get the count of selection options in multiple selection list? I am using jquery validation plugin.

Upvotes: 56

Views: 96280

Answers (5)

Ahmed khaled
Ahmed khaled

Reputation: 61

I use select2 for multi select and this worked with me

 $("#selectedItems").val().length;

note that 'selectedItems' is the id of multiselect list

Upvotes: 0

Strabek
Strabek

Reputation: 2511

For checkboxes I'm using:

$('input[name="InputName"]:checked').size()

Upvotes: 1

Nick Craver
Nick Craver

Reputation: 630637

You can use the :selected selector to select them, then get the .length, like this:

var count = $("#mySelect :selected").length;

Upvotes: 111

halfdan
halfdan

Reputation: 34274

You can get the number of selected items in the multiselect with the following:

$('#selectList :selected').length

Where #selectList is the id of your .

Upvotes: 4

Prashant Lakhlani
Prashant Lakhlani

Reputation: 5806

var n = $("input:checked").length; see this for more detail: http://api.jquery.com/checked-selector/

Upvotes: 3

Related Questions