#1
  1. a lazy guy
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2001
    Location
    Hong Kong
    Posts
    99
    Rep Power
    13

    Javascript: SelectedIndex


    Hi Guyz,

    I tried to print the selected index of a multiple selection box, but it prints the previous selected index only.
    Here is the codes:

    <html>
    <script>
    function displayIndex(obj)
    {
    alert(obj.selectedIndex);
    }
    </script>
    <SELECT NAME="mylist" MULTIPLE="true" SIZE="5" onClick="displayIndex(this)">
    <OPTION VALUE="MU">Manchester United</OPTION>
    <OPTION VALUE="LP">Liverpool</OPTION>
    <OPTION VALUE="AR">Arsenal</OPTION>
    </SELECT>
    </html>


    Anyone got a solution here?
    Thanks.
  2. #2
  3. a lazy guy
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2001
    Location
    Hong Kong
    Posts
    99
    Rep Power
    13

    solved


    the above problem is solved by replacing the event OnClick to OnChange
  4. #3
  5. No Profile Picture
    Senior Member
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Nov 2001
    Location
    Boston Ma.
    Posts
    1,529
    Rep Power
    0
    Hi


    You can't do that......... value has not been set until it is clicked, so it will always result to -1 on first click....

    onChange


    <html>
    <script>
    function grab_it() {
    var myindex=mylist.selectedIndex;
    var myresult=mylist.value;
    alert("SELECTED INDEX = " + myindex + "\nSELECTED VALUE = " + myresult);
    }
    </script>

    <SELECT NAME="mylist" MULTIPLE="true" SIZE="5" onChange="grab_it();">
    <OPTION VALUE="MU">Manchester United</OPTION>
    <OPTION VALUE="LP">Liverpool</OPTION>
    <OPTION VALUE="AR">Arsenal</OPTION>
    </SELECT>
    </html>


    F!
    Last edited by Fataqui; January 16th, 2002 at 12:47 AM.

IMN logo majestic logo threadwatch logo seochat tools logo