SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot websalacarta's Avatar
    Join Date
    May 2003
    Location
    Spain
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    options.selectedIndex question

    Hi,

    I have one select, I can say wich element is selected:

    Code:
    <select name="myselect">
      <option value="100">A</option>
      <option value="101">B</option>
      <option value="102">C</option>
    </select>
    this line will select the option B
    Code:
    myselect.options.selectedIndex = 1;
    but, how can I do if I want to select the option B without knowing itīs position?
    I mean, I want to select the option B and the value 101, but I donīt know itīs position

    any idea?

    thanks

  2. #2
    SitePoint Zealot websalacarta's Avatar
    Join Date
    May 2003
    Location
    Spain
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi, Iīve seen my question is the same that this one:

    http://www.sitepoint.com/forums/showthread.php?t=189874

    any idea?

  3. #3
    SitePoint Zealot websalacarta's Avatar
    Join Date
    May 2003
    Location
    Spain
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    and finally I found one solution:

    http://www.sitepoint.com/forums/showthread.php?t=111403

    isnīt sitepoint wonderful? :-)

    thanks very much

  4. #4
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Portsmouth UK
    Posts
    1,499
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Done it now

    PHP Code:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
        "http://www.w3.org/TR/html4/loose.dtd"
    >

    <
    html>

    <
    head>
      <
    title></title>
    <
    script language="JavaScript" type="text/javascript">
    <!--

    function 
    FindText(){
     
    Sel=document.getElementById('myselect');
     for (
    i=0;i<Sel.length;i++){
      if (
    Sel.options[i].text=='B'){
       
    alert('Index B is '+i);
      }
     }
    }

    function 
    FindValue(){
     
    Sel=document.getElementById('myselect');
     for (
    i=0;i<Sel.length;i++){
      if (
    Sel.options[i].value=='102'){
       
    alert('Index of C is '+i);
      }
     }
    }

    //-->
    </script></head>

    <body>
    <select name="myselect">
      <option value="100">A</option>
      <option value="101">B</option>
      <option value="102">C</option>
    </select>
    <br>
    <input type="button" name="" value="find index of B using text" onclick="FindText();" >
    <br>
    <input type="button" name="" value="find index of C using value" onclick="FindValue();" >
    <br>

    </body>

    </html> 


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
  •