enter image description here

next to the Search Results: I want to add a number of counts of the list

I need to display number of items selected in the multi-select list next to the Total Selected Counts and I'm using <apex:outputText > but not sure how to display the counter next to it?

    <apex:selectList value="{!RightSelectedList}" multiselect="true"   style="width:100%" size="10"  >
    <apex:selectOptions value="{!RightOptionList}"   />
    </apex:selectList><apex:outputText >Total Selected Counts:</apex:outputText>
  • Do you want the number selected the last time the page was saved (do-able with Visualforce) or do you want the number currently selected in the UI i.e. when the user has changed the selection but not yet saved the page (that required JavaScript)?
    – Keith C
    Aug 21, 2015 at 22:30
  • The last part, selected in the UI
    – Nick
    Aug 21, 2015 at 22:31

2 Answers 2


Here is a way to count and display the selected options in HTML using jQuery:

<select class="selectMarker" multiple="multiple">
    <option selected="selected">Shrubs</option>
    <option selected="selected">Bushes</option>
<div class="countMarker"></div>

<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
(function($) {
    function updateCount() {
        var n = $('.selectMarker option:selected').length;
    $('.selectMarker').change(function() {


To convert this to Visualforce, just copy the script content and use the styleClass attribute on the Visualforce tags where class is used above on the HTML elements.

  • hmm... i am not sure if this is the right way of doing and since I already have list when the user adds in the multi-select, what i'm looking a way to display the list.count in the visualforce page... it will be much easier then using jquery which i think its not overkill in my opinion.
    – Nick
    Aug 21, 2015 at 23:14
  • you see I already know what has been selected RightSelectedList so what I need on the apex side to count and display on the VFP, thats what i am looking a way to display the value
    – Nick
    Aug 21, 2015 at 23:15
  • @AbuHamzah The above works when the page is first opened and also works immediately the selection is changed. Not sure why you want the count, but if you only calculate it when the page is opened and the list is editable then the count will be wrong immediately the user edits it and will only be correct after a save and a re-opening of the page. But to compute the count in Apex (avoiding jQuery) you can split the picklist value on the separator for multi-select picklists which is a semi-colon and take the size of the resulting array.
    – Keith C
    Aug 21, 2015 at 23:25
  • I will post you the screen shot to make more clear
    – Nick
    Aug 21, 2015 at 23:53
  • I have updated my question with an image to give more context of my question. - thanks.
    – Nick
    Aug 22, 2015 at 0:02

Here is how I able to achieve:


public string RightSelectedListCount()
    return RightSelectedList.size();

Visualforce Page:


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .