click event in SELECT element

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • araripe

    click event in SELECT element

    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.onclic k = fnc_document_cl ick
    ....

    function fnc_document_cl ick(){
    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

  • Thomas 'PointedEars' Lahn

    #2
    Re: click event in SELECT element

    araripe wrote:
    [color=blue]
    > document.onclic k = fnc_document_cl ick
    > ...
    >
    > function fnc_document_cl ick(){
    > if(window.event .srcElement.id == "my_lst"){
    > ......
    > }
    > }[/color]

    This is inefficient error-prone proprietary referencing and IE-only.
    Don't use it.
    [color=blue]
    > 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?[/color]

    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(th is);">
    ...
    </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

    Comment

    Working...