By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,288 Members | 3,027 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,288 IT Pros & Developers. It's quick & easy.

which <option> was clicked on?

P: n/a
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
Jul 20 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a

"Bart Van der Donck" <ba**@nijlen.com> schreef in bericht
news:b5**************************@posting.google.c om...
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

Jul 20 '05 #2

P: n/a
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>
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 20 '05 #3

P: n/a

"Evertjan." <ex**************@interxnl.net> schreef in bericht
news:Xn********************@194.109.133.29...

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

Jul 20 '05 #4

P: n/a
Janwillem Borleffs wrote on 06 sep 2003 in comp.lang.javascript:

"Evertjan." <ex**************@interxnl.net> schreef in bericht
news:Xn********************@194.109.133.29...

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
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 20 '05 #5

P: n/a
Thanks Janwillem & Evertjan,

Groetjes uit Belgie
Bart
Jul 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.