which <option> was clicked on?

  • Thread starter Bart Van der Donck
  • Start date
B

Bart Van der Donck

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(this.value)">
<option value="a">111
<option value="b">222
<option value="c">333
</select>
</body>
</html>


Many thanks
Bart
 
J

Janwillem Borleffs

Bart Van der Donck said:
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:

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(selectedIndex)">
<option value="a">111
<option value="b">222
<option value="c">333
</select>
</body>
....

JW
 
E

Evertjan.

Janwillem Borleffs wrote on 06 sep 2003 in comp.lang.javascript:
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(selectedIndex)">
<option value="a">111
<option value="b">222
<option value="c">333
</select>
</body>

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(this,selectedIndex)">
<option value="a">111
<option value="b">222
<option value="c">333
</select>
</body>
 
J

Janwillem Borleffs

Evertjan. said:
IE:

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

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
 
E

Evertjan.

Janwillem Borleffs wrote on 06 sep 2003 in comp.lang.javascript:
Evertjan. said:
IE:

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

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

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

didn't know about .text, tnx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,188
Latest member
Crypto TaxSoftware

Latest Threads

Top