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?

1
  • Is the ID for the ListBox StatesID or statesMailing? Commented Jul 26, 2013 at 16:02

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Try this :

$(document).ready(function() {
    $('#StatesID').click(countStates);
});

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.