SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2002
    Location
    Fort Lauderdale
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    get index of select from value

    I have a select like this
    Code:
    <select name=sel>
      <option value='1' selected>1
      <option value='2'>2
      <option value='3'>3
    </select>
    In a javascript function, I need to get the index value of the select where value='3'. So that index would be 2.

    I'm looking for something like
    i = document.forms['frm'].elements['sel'].options['3'].index;
    I know that doesn't work. So does anyone know how to get an index value of a select when you only know the value of the option that you want the index for?

    PS I'm not looking for selectedIndex.

    www.unpossiblemedia.com - A Fort Lauderdale based multimedia company.

  2. #2
    SitePoint Zealot
    Join Date
    Jul 2002
    Location
    Fort Lauderdale
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If what I'm asking for is not possible, then how about this..
    Is there a way to change which item is selected if you know the value of the item you want selected but not the index of it?

    www.unpossiblemedia.com - A Fort Lauderdale based multimedia company.

  3. #3
    SitePoint Wizard silver trophy
    Join Date
    May 2003
    Posts
    1,843
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    function getOptionFromValue(oSelect, optval) {
    var opt, i = 0;
    while (opt = oSelect.options[i++])
    if (opt.value == optval) return opt.index;
    return -1;
    }

    getOptionFromValue(document.frm.sel, '3');
    Last edited by adios; May 30, 2003 at 18:45.

  4. #4
    SitePoint Zealot
    Join Date
    Jul 2002
    Location
    Fort Lauderdale
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Perfect, thanks.

    www.unpossiblemedia.com - A Fort Lauderdale based multimedia company.


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
  •