Results 1 to 2 of 2

Thread: List Box Count with a button

  1. #1
    Join Date
    Nov 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default List Box Count with a button

    Hey everyone,

    I'm looking for a way to count the list box items once the user has click them and with a button display how many that the user selected.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    There is no list box. I think you are referring to a select element with the multiple attribute, but I cannot be sure. If so, this will work:

    Code:
    <script type="text/javascript">
    function howMany(el){
    for (var s = 0, i = el.options.length - 1; i > -1; --i)
    if(el.options[i].selected) ++s;
    return s;
    }
    </script>
    <select id="mySel" multiple name="" onchange="alert(howMany(this));">
    <option value="">1</option>
    <option value="">2</option>
    <option value="">3</option>
    <option value="">4</option>
    <option value="">5</option>
    </select><br>
    <input type="button" onclick="alert(howMany(document.getElementById('mySel')));" value="How Many">
    If you want more help, please show us the markup and code you are working with, or:

    Please post a link to the page on your site that contains the problematic code so we can check it out.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •