473,320 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Index of Clicked option in multiple select

Hi,

I want to know the index of the option that a user clicks on in a
multiple-select object (regardless of whether he selected or deselected
it). This seems fairly simple but I can't seem to figure out how to do
it. Does anybody know how to do this?

Thanks.
Puneet

Jan 20 '06 #1
1 7309
pu***********@wipro.com wrote:
Hi,

I want to know the index of the option that a user clicks on in a
multiple-select object (regardless of whether he selected or deselected
it). This seems fairly simple but I can't seem to figure out how to do
it. Does anybody know how to do this?


Option elements can have onclick attributes and they also have in index
property, so you can do:

<option onclick="alert(this.index + ' selected? ' + this.selected);">
in browsers that conform to HTML 4 and DOM 2. However, not all browsers
fully implement these standards.

For a single select, whatever option is clicked on is selected - the
only way to de-select it is to either select some other option or reset
the select. Therefore, whatever option is selected is the one that was
clicked on - looking at the select's selectedIndex property will tell
you which option that is.

You can also use the the click event's attributes to find out what
element was clicked on (the target/srcElement for W3C/IE event models) -
but since IE doesn't seem to have an onclick attribute for options, it
tells you that event.srcElement is the select, not the option that was
clicked on.

For a multiple select and W3C compliant browser, you can use either the
options onclick attribute (preferred) or event attributes (not preferred
but helpful sometimes), but with IE you're out of luck.

Something to play with:
<script type="text/javascript">

function optClickedOn(e)
{
var e = e || window.event;
var tgt = e.target || e.srcElement;
var t = '';
if (tgt){
if (tgt.nodeName) {
t += tgt.nodeName;
}
if ('number' == typeof tgt.index) {
t += ' index ' + tgt.index + '.';
}
if ('boolean' == typeof tgt.selected) {
t += ' It is ' + ((tgt.selected)?'':'not ') + 'selected.';
}
}
document.getElementById('msg').innerHTML = t;
}

</script>

<div>You clicked on: <span id="msg"></span></div>
<select onclick="optClickedOn(event);" multiple>
<option>opt 01
<option>opt 02
<option>opt 03
</select>
<br>
<select>
<option onclick="alert(this.index);">opt 01
<option onclick="alert(this.index);">opt 02
<option onclick="alert(this.index);">opt 03
</select>

--
Rob
Jan 22 '06 #2

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

Similar topics

0
by: Guy Deprez | last post by:
Hi, i'm having a problem to create indexes. STEP 1 ----------- Connection is OK (you can find the string at the end of the message) Table ("Couleurs") creation is OK STEP 2. Index Creation
5
by: Bart Van der Donck | last post by:
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...
4
by: Ferd Berfel | last post by:
given this code: <select name="mySelect" size="3" multiple> <option>one</option> <option>two</option> <option>three</option> <option>four</option> <option>five</option> <option...
1
by: Robert Oschler | last post by:
I read a while back that MySQL will only use one index per query. (If this is not so, please tell me and point me to a doc that gives a good explanation of MySQL's current index usage policy). ...
7
by: smash2004 | last post by:
i have a select field with multiple enabled so user can select multiple options... is it possible to get option that was clicked last...i need this because i need to check if this option was...
7
by: Stefan Mueller | last post by:
I choose 'Entry 4' and click then on the button 'Set' to set the index to 'Entry 2'. If you press now the cursor down key 'Entry 5' instead of 'Entry 3' shows up with Mozilla Firefox. With Internet...
2
by: areef.islam | last post by:
Hi, I am kinda new to javascript and I am having this problem with selecting multiple options from a select tag. Hope someone can help me out here. here is my code...
5
by: =?Utf-8?B?U2NhbmJveQ==?= | last post by:
Guyz, Whatever happened to the 'Index' property for a control, that used to be present in VB 3.0 / 4.0 / 5.0 / 6.0 and which now seems to be missing from VBE 2005? I need to be able to make 4...
5
by: lightgram | last post by:
Hi I have a problem, which after browsing through Google, seems to be fairly common. However having tried most suggestions I am still getting the problem. I have a menu bar across the top...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.