which <option> was clicked on?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bart Van der Donck

    which <option> was clicked on?

    Hello,

    My hair falls out!
    Is it possible to retrieve the "ordering value" of the <option> that
    was clicked inside a range of options ?
    In other words, with something like
    <option value="myvalue" >mytext</option>
    is it possible to know the position of that <option> between the other
    <option>s and retrieve that value through javascript ?

    Here is the simplified code, where "myvar" is the variable that should
    somehow become the position of the clicked option:

    <html>
    <head>
    <script>
    function g(myvar)
    {
    alert('You clicked on the ' + myvar + 'th option.')
    }
    </script>
    </head>

    <body>
    <select name="n" onChange="g(thi s.value)">
    <option value="a">111
    <option value="b">222
    <option value="c">333
    </select>
    </body>
    </html>


    Many thanks
    Bart
  • Janwillem Borleffs

    #2
    Re: which &lt;option&g t; was clicked on?


    "Bart Van der Donck" <bart@nijlen.co m> schreef in bericht
    news:b5884818.0 309060438.24db8 004@posting.goo gle.com...[color=blue]
    > Hello,
    >
    > My hair falls out!
    > Is it possible to retrieve the "ordering value" of the <option> that
    > was clicked inside a range of options ?
    > In other words, with something like
    > <option value="myvalue" >mytext</option>
    > is it possible to know the position of that <option> between the other
    > <option>s and retrieve that value through javascript ?
    >
    > Here is the simplified code, where "myvar" is the variable that should
    > somehow become the position of the clicked option:
    >[/color]

    I think that you are looking for selectedIndex:
    ....
    <script>
    function g(myvar)
    {
    // selectedIndex starts off with 0, so add 1 to meet
    // human logics
    alert('You clicked on the ' + ++myvar + 'th option.')
    }
    </script>
    </head>

    <body>
    <select name="n" onChange="g(sel ectedIndex)">
    <option value="a">111
    <option value="b">222
    <option value="c">333
    </select>
    </body>
    ....

    JW



    Comment

    • Evertjan.

      #3
      Re: which &lt;option&g t; was clicked on?

      Janwillem Borleffs wrote on 06 sep 2003 in comp.lang.javas cript:
      [color=blue]
      > I think that you are looking for selectedIndex:
      > ...
      > <script>
      > function g(myvar)
      > {
      > // selectedIndex starts off with 0, so add 1 to meet
      > // human logics
      > alert('You clicked on the ' + ++myvar + 'th option.')
      >}
      > </script>
      > </head>
      >
      > <body>
      > <select name="n" onChange="g(sel ectedIndex)">
      > <option value="a">111
      > <option value="b">222
      > <option value="c">333
      > </select>
      > </body>
      >[/color]

      IE:

      <script>
      function g(x,myvar) {
      alert('the optionvalue was: ' + x.childNodes[myvar+1].value +
      '\nthe text was: ' + x.childNodes[myvar+1].innerText)
      }
      </script>

      <select name="n" onChange="g(thi s,selectedIndex )">
      <option value="a">111
      <option value="b">222
      <option value="c">333
      </select>
      </body>


      --
      Evertjan.
      The Netherlands.
      (Please change the x'es to dots in my emailaddress)

      Comment

      • Janwillem Borleffs

        #4
        Re: which &lt;option&g t; was clicked on?


        "Evertjan." <exjxw.hannivoo rt@interxnl.net > schreef in bericht
        news:Xns93EEB2C 5A9F98eejj99@19 4.109.133.29...[color=blue]
        >
        > IE:
        >
        > <script>
        > function g(x,myvar) {
        > alert('the optionvalue was: ' + x.childNodes[myvar+1].value +
        > '\nthe text was: ' + x.childNodes[myvar+1].innerText)
        > }
        > </script>[/color]

        Why using DOM when the Form object already provides you with cross-browser
        compliant properties?

        function g(x,myvar) {
        alert('the optionvalue was: ' + x[myvar].value +'\nthe text was: ' +
        x[myvar].text)
        }


        JW



        Comment

        • Evertjan.

          #5
          Re: which &lt;option&g t; was clicked on?

          Janwillem Borleffs wrote on 06 sep 2003 in comp.lang.javas cript:
          [color=blue]
          >
          > "Evertjan." <exjxw.hannivoo rt@interxnl.net > schreef in bericht
          > news:Xns93EEB2C 5A9F98eejj99@19 4.109.133.29...[color=green]
          >>
          >> IE:
          >>
          >> <script>
          >> function g(x,myvar) {
          >> alert('the optionvalue was: ' + x.childNodes[myvar+1].value +
          >> '\nthe text was: ' + x.childNodes[myvar+1].innerText)
          >> }
          >> </script>[/color]
          >
          > Why using DOM when the Form object already provides you with
          > cross-browser compliant properties?[/color]

          dunno
          [color=blue]
          > function g(x,myvar) {
          > alert('the optionvalue was: ' + x[myvar].value +'\nthe text was: '
          > + x[myvar].text)
          >}[/color]

          didn't know about .text, tnx


          --
          Evertjan.
          The Netherlands.
          (Please change the x'es to dots in my emailaddress)

          Comment

          • Bart Van der Donck

            #6
            Re: which &lt;option&g t; was clicked on?

            Thanks Janwillem & Evertjan,

            Groetjes uit Belgie
            Bart

            Comment

            Working...