Can't get selectedIndex property for listbox

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Russell Campbell

    #1

    Can't get selectedIndex property for listbox

    Posting again, since my first attempt never appeared:

    In the onClick method of a listbox, I am attempting to retrieve the
    selectedIndex property. The listbox is set up to allow multiple selections.
    Unfortunately, the property returns -1. Clicking again returns the proper
    result. Yet when I do the same thing on the same listbox after changing it
    to allow only one entry to be selected, it always returns the proper result
    on the first click. This is a pain. Is there any way to get the proper
    value during the onClick when the listbox is set to allow multiple entries?

    Thanks,

    Russell Campbell



  • Grant Wagner

    #2
    Re: Can't get selectedIndex property for listbox

    Russell Campbell wrote:
    [color=blue]
    > Posting again, since my first attempt never appeared:
    >
    > In the onClick method of a listbox, I am attempting to retrieve the
    > selectedIndex property. The listbox is set up to allow multiple selections.
    > Unfortunately, the property returns -1. Clicking again returns the proper
    > result. Yet when I do the same thing on the same listbox after changing it
    > to allow only one entry to be selected, it always returns the proper result
    > on the first click. This is a pain. Is there any way to get the proper
    > value during the onClick when the listbox is set to allow multiple entries?
    >
    > Thanks,
    >
    > Russell Campbell[/color]

    <form name="myForm">
    <select multiple onchange="alert (getSelectedInd exes(this));">
    <option value="1">1</option>
    <option value="1">1</option>
    <option value="1">1</option>
    <option value="1">1</option>
    <option value="1">1</option>
    <option value="1">1</option>
    </select>
    </form>
    <script type="text/javascript">
    function getSelectedInde xes(selObj) {
    if (selObj.type == 'select-one') {
    return new Array(selObj.se lectedIndex);
    } else {
    var indexes = new Array();
    for (var i = 0; i < selObj.options. length; i++) {
    if (selObj.options[i].selected) {
    indexes.push(i) ;
    }
    }
    return indexes;
    }
    }
    </script>

    The function returns an array. Using alert() calls .toString() on the returned
    on the array, which returns [theArray].join(","), that's why you get a
    comma-delimited list in the alert().

    To use the returned array to do something useful, you'd have to iterate through
    it.

    The other alternative is to have the function return an associative array:

    function getSelectedInde xes(selObj) {
    var indexes = new Object();
    if (selObj.type == 'select-one') {
    indexes['idx' + selObj.selected Index] = true;
    return indexes;
    } else {
    for (var i = 0; i < selObj.options. length; i++) {
    if (selObj.options[i].selected) {
    indexes['idx' + i] = true;
    }
    }
    return indexes;
    }
    }

    Now you could test for a particular item being selected directly:

    onchange="if (getSelectedInd exes(this)['idx4']) { alert('4th index is
    selected');"

    --
    | Grant Wagner <gwagner@agrico reunited.com>

    * Client-side Javascript and Netscape 4 DOM Reference available at:
    *


    * Internet Explorer DOM Reference available at:
    *
    http://msdn.microsoft.com/workshop/a...ence_entry.asp

    * Netscape 6/7 DOM Reference available at:
    * http://www.mozilla.org/docs/dom/domref/
    * Tips for upgrading JavaScript for Netscape 6/7 and Mozilla
    * http://www.mozilla.org/docs/web-deve...upgrade_2.html


    Comment

    • Lee

      #3
      Re: Can't get selectedIndex property for listbox

      Russell said:[color=blue]
      >
      >Posting again, since my first attempt never appeared:
      >
      >In the onClick method of a listbox, I am attempting to retrieve the
      >selectedInde x property. The listbox is set up to allow multiple selections.
      >Unfortunatel y, the property returns -1. Clicking again returns the proper
      >result. Yet when I do the same thing on the same listbox after changing it
      >to allow only one entry to be selected, it always returns the proper result
      >on the first click. This is a pain. Is there any way to get the proper
      >value during the onClick when the listbox is set to allow multiple entries?[/color]

      That's like complaining that you are able to get the birthdate of
      an individual, but you're not getting a value when you ask for the
      birthdate of a household.

      If more than one item can be selected, the concept of a selectedIndex
      doesn't really make sense, does it? You loop through the options to
      see which have the "selected" attribute set to true.

      Comment

      • Lasse Reichstein Nielsen

        #4
        Re: Can't get selectedIndex property for listbox

        "Russell Campbell" <nospam@byteme. com> writes:
        [color=blue]
        > Posting again, since my first attempt never appeared:
        >
        > In the onClick method of a listbox, I am attempting to retrieve the
        > selectedIndex property.[/color]

        You should probably use "onchange" instead. The first click might just
        expand the selection box, but haven't selected anything yet.

        /l
        --
        Lasse Reichstein Nielsen - lrn@hotpop.com
        Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
        'Faith without judgement merely degrades the spirit divine.'

        Comment

        Working...