1

As in topic - how to get selected items count in ListBox with jQuery, while user is selecting new item?

I have these code:

@Html.ListBoxFor(x => Model.StatesID, Model.States,
        new { @class = "chzn-select", @id="StatesID", data_placeholder = "Choose...", style = "width:350px;" })


function countStates() {
    var count = $("#StatesID:selected").length;
    alert(count);

}

I can get count of selected items in onclick input action, but how to connect this js function with onclick event, or maybe other event, when user is selecting new item in my listbox?

tereško
  • 58,060
  • 25
  • 98
  • 150
whoah
  • 4,363
  • 10
  • 51
  • 81

2 Answers2

1

Adding my comment as answer....

If it is a multi-select, you will want to use....

$('#StatesID').on('change', function(){
    countStates();
});

otherwise click does not fire if the user selects more that one option at a time.

Schmalzy
  • 17,044
  • 7
  • 46
  • 47
0

Try this :

$(document).ready(function() {
    $('#StatesID').click(countStates);
});
  • 1
    If it is a milti-select, you will want to use `$('#StatesID').on('change', function(){countStates();});` otherwise click does not fire if the user selects more that one option at a time. – Schmalzy Jul 26 '13 at 15:59