Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Dec 2006
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts

    selectedindex doesn't seem to work

    I'm trying to find the selectedindex of the option which is selected by the user, but the following code show undefined whenever change occurs

    Code:
    <html>
    <body>
    
    <form>
    <select id="ddlCar" name="cars" onchange="alert( String( document.getElementById( 'ddlCar' ).selectedindex ) );">
      <option value="volvo">Volvo</option>
      <option value="saab">Saab</option>
      <option value="fiat" selected="selected">Fiat</option>
      <option value="audi">Audi</option>
    </select>
    </form>
    
    </body>
    </html>

  • #2
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You just have to capitalize the 'i' to

    .selectedIndex

    Subtile but important.

    david_kw

  • #3
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    You may use the this self reference to simplify the code

    onchange="alert((this.selectedIndex).toString())"
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #4
    New Coder
    Join Date
    Dec 2006
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool

    Thanks


  •  

    Posting Permissions

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