click event in SELECT element

A

araripe

Hi,

Does someone can help me with the following problem?
I have a SELECT form tag, with some OPTIONS elements. I am using the
following code to detect a click in the SELECT.

....
document.onclick = fnc_document_click
....

function fnc_document_click(){
if(window.event.srcElement.id == "my_lst"){
......
}
}

How can I avoid the "if" if user click in a area of SELECT out of the
options elements? (The SELECT object is higher than the the goups of
OPTIONS its contains)

In other words, how can detect if the click Iinside the SELECT element)
happens in a OPTION element or not?

Thanks in advance
 
T

Thomas 'PointedEars' Lahn

araripe said:
document.onclick = fnc_document_click
...

function fnc_document_click(){
if(window.event.srcElement.id == "my_lst"){
......
}
}

This is inefficient error-prone proprietary referencing and IE-only.
Don't use it.
How can I avoid the "if" if user click in a area of SELECT out of the
options elements? (The SELECT object is higher than the the goups of
OPTIONS its contains)

In other words, how can detect if the click Iinside the SELECT element)
happens in a OPTION element or not?

You don't. Handle the event where it occurs and you don't have
to test for anything but the target elements:

function foo(target)
{
// handle event
}

<select ... onclick="foo(this);">
...
</select>

Note that it does not work if client-side JS is not supported, so
be sure to provide a viable alternative (probably server-side).


PointedEars
 

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

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,119
Latest member
IrmaNorcro
Top