473,473 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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(this.value)">
<option value="a">111
<option value="b">222
<option value="c">333
</select>
</body>
</html>
Many thanks
Bart
Jul 20 '05 #1
5 2558

"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
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

"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
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
Thanks Janwillem & Evertjan,

Groetjes uit Belgie
Bart
Jul 20 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Ang Talunin | last post by:
Hey, I wondering if it's possible to retrieve all the <option>-fields from a <select> when posting a <form> to a php file Example: I've got a form like this: <form action = phpfile.php...
6
by: Hal Vaughan | last post by:
I'm using KDE on Linux, with Konqueror as the testing browser for this project. I've recently upgraded, so I realize some of the bugs I'm dealing with may or may not be my program, and could also...
6
by: Chris Fink | last post by:
Does anyone know it is possible to include a small image(.gif .jpeg) within a <SELECT><option> so that the user would see the option text as well as a little image(icon) in the option? I know this...
6
by: joseph.lindley | last post by:
Forgive me for I am a bit of a web-dev novice - but I'm not doing too bad. I'm currently working with a bit of javascript to dynamically add <option>s into a select box. My code currently works...
1
by: frey | last post by:
i tried to create a dropdown menu and use the option as a control to change content inside another text area the code is like this: <select name="xxxx"><option onclick="changeunitprice(29.87)"...
4
by: Man-wai Chang | last post by:
-- iTech Consulting Co., Ltd. Expert of ePOS solutions Website: http://www.itech.com.hk (IE only) Tel: (852)2325 3883 Fax: (852)2325 8288
4
by: pplers | last post by:
Here is config.php: <?php //The vars are all ok. $dbhost = 'localhost'; $dbname = 'forum'; $dbuser = 'toor'; $dbpass = ''; ?> Here is a part of functions.php: <? require "config.php"; //It...
14
by: The Natural Philosopher | last post by:
This is a nasty one and I can't see my way out of it. I have a bunch of select statements in a form, and each select statement has an onchange="do_something(this)" in it, and this works...
9
by: eihabisaac | last post by:
hey every body how can i get using javascript the selected option <option> ID or Value because when ever i clicked it i get the <select> id and i dont want that <select name="list" id="list"...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.