473,664 Members | 2,995 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7335
pu***********@w ipro.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.srcElemen t 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.getEle mentById('msg') .innerHTML = t;
}

</script>

<div>You clicked on: <span id="msg"></span></div>
<select onclick="optCli ckedOn(event);" multiple>
<option>opt 01
<option>opt 02
<option>opt 03
</select>
<br>
<select>
<option onclick="alert( this.index);">o pt 01
<option onclick="alert( this.index);">o pt 02
<option onclick="alert( this.index);">o pt 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
2809
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
2572
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 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 ?
4
2880
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 selected>six</option> <option selected>seven </option>
1
2410
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). I'm using MySQL 4.2.x. Here's my dilemma. 1) --------- I have two tables that have records with a FULLTEXT index text field in each of them. The problem is the relationship between the tables is a
7
1757
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 already selected to deselect it thx for replys
7
1941
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 Explorer and Opera it works fine; 'Entry 3' shows up. Is this a bug within Mozilla Firefox or do I have any possibility to change this wrong behavior? Stefan ========================
2
3650
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 /////////////////////////////////////////////////////////////////////////////////////// <form action="whatever.php" method="post"> <select name="zip_code" onchange="makeRequest('getCity.php?state='+this.form.zip_code.options.value)" multiple="multiple" size="20">
5
1321
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 command buttons behave in the same way, the 'Index' property indicating which command button I clicked on.
5
14238
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 of my page. As I click on each menu option a further list of links is displayed below it. Now in IE7 where I click on a menu option that has a large number of links below it it appears layered correctly over the top of the controls below.
0
8348
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8778
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8549
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8636
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7375
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6187
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4351
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2764
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 we have to send another system
2
2003
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.