How to know which option in select field was clicked last?

S

smash2004

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
 
R

RobG

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

It's a bad idea to change the standard behaviour of a GUI element.
Users know to click once to select an item, ctrl+click selects an
extra one, shift+click selects a range, etc. If you change that, you
will probably confuse people.

Some use 'sticky keys' to do selections, other use voice activated
interfaces, others like to use purely keyboard navigation and not a
mouse. You can't possibly code for all those situations.

Those who learn your modified behaviour will then likely be stumped
when they go to some other site that uses the standard interface.

To achieve what you are after, cancel the default behaviour, then
simply toggle the selected property of whichever option is clicked on
- there is no need to remember the last one that was clicked on.

And expect your users to be utterly confused.
 
S

smash2004

1.) I need to have this kind of behavior and period.

2.) I need to know what was the option that was clicked last because i
need to check if this option is already selected..if it is already
selected then i have to deselect it..I don't need to do this for every
other option except for the one that was selected last.

Get it?
 
S

Stephen Chalmers

1.) I need to have this kind of behavior and period.

2.) I need to know what was the option that was clicked last because i
need to check if this option is already selected..if it is already
selected then i have to deselect it..I don't need to do this for every
other option except for the one that was selected last.

Get it?
Are you saying that if the user clicks on a currently selected option, that
option should be deselected, when normally nothing would happen?

Unfortunately, I.E does not support onclick for individual select options,
without which I can't see how to implement that behaviour.

Of course under Mozilla or Opera it's a doddle. Try this example, which
demonstrates two alternative select behaviours:

<HTML>
<BODY>

Normal behaviour preserved, except click toggles current option.
<FORM name='f1'>
<SELECT multiple name='s1'>
<OPTION value='red'>red
<OPTION value='green'>green
<OPTION value='blue'>blue
<OPTION value='black'>black
</SELECT>
</FORM>
<BR><BR>

Multiple Select using mouse only; shift/ctrl do not override.
<FORM name='f2'>
<SELECT multiple name='s1'>
<OPTION value='red'>red
<OPTION value='green'>green
<OPTION value='blue'>blue
<OPTION value='black'>black
</SELECT>
</FORM>

<SCRIPT type='text/javascript'>

function monitorSelect( objRef, sel, multi )
{
objRef=this;
this.sel=sel;
this.multi=multi;
this.optStates=[];

for(var i=0; i<this.sel.options.length; i++)


this.optStates=this.sel.options.selected;
this.sel.options.onclick=function(){ objRef.recordStates(this) };
}
}

monitorSelect.prototype.recordStates=function(opt)
{
for(var i=0; i<this.sel.options.length; i++)
{
if(opt==this.sel.options)


if(this.optStates)
this.sel.options.selected=false;
}
else
if(this.multi)
this.sel.options.selected=this.optStates;

this.optStates=this.sel.options.selected;
}
}

var selObj1=new monitorSelect(selObj1, document.forms.f1.s1, false);
var selObj2=new monitorSelect(selObj2, document.forms.f2.s1, true);

</SCRIPT>

</BODY>
 
R

RobG

Stephen said:
Are you saying that if the user clicks on a currently selected option, that
option should be deselected, when normally nothing would happen?

Unfortunately, I.E does not support onclick for individual select options,
without which I can't see how to implement that behaviour.

Of course under Mozilla or Opera it's a doddle. Try this example, which
demonstrates two alternative select behaviours:

You've made an admirable ('zilla only) attempt, but the arrow keys
defeat you. Click on two 'f2' options in sequence, then use an up (or
down) arrow key - only one option remains selected.

Subsequent clicks cause behaviour that, so someone who doesn't know what
is happening under the hood, is quite confusing.

Using script to re-program default UI behaviour nearly always fails.
On-screen help is simpler and less prone to error:

"Use control + click to select multiple options"

[...]
 
S

Stephen Chalmers

RobG said:
You've made an admirable ('zilla only) attempt, but the arrow keys
defeat you. Click on two 'f2' options in sequence, then use an up (or
down) arrow key - only one option remains selected.

Subsequent clicks cause behaviour that, so someone who doesn't know what
is happening under the hood, is quite confusing.

Using script to re-program default UI behaviour nearly always fails.

I knew that, but I set out to demonstrate only the requested mouse-based selection,
which in practice is never going to be implemented, at least not with select boxes.
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top