Passing back an element that's been OnClicked

Discussion in 'Javascript' started by stevewy@hotmail.com, Aug 17, 2006.

  1. Guest

    If I surround a number of form elements with a <DIV> tag and put an
    OnClick in the DIV tag, if I click on any of the form elements
    (checkboxes and radios, in this instance) inside the DIV, is there a
    way of telling which element has been clicked on/checked/unchecked,
    short of putting an onclick into each individual element? Is there a
    property that can be read that stores what was last Clicked on? Even
    just the NAME or ID?
    , Aug 17, 2006
    #1
    1. Advertising

  2. RobG Guest

    wrote:
    > If I surround a number of form elements with a <DIV> tag and put an
    > OnClick in the DIV tag, if I click on any of the form elements
    > (checkboxes and radios, in this instance) inside the DIV, is there a
    > way of telling which element has been clicked on/checked/unchecked,
    > short of putting an onclick into each individual element? Is there a
    > property that can be read that stores what was last Clicked on? Even
    > just the NAME or ID?


    The event object associated with the onclick has either a srcElement
    (IE) or target (W3C) property that is a reference to the element that
    originally fired the onclick event:

    <script type="text/javascript">

    function foo(e){
    var tgt = e.target || e.srcElement;
    alert('You clicked on a ' + tgt.nodeName);
    }

    </script>


    <div style="width: 15em; height: 5em; background-color: #def;"
    onclick="foo(event);">
    <p>Here is a paragraph <span style="color: blue;">And
    here is a span <b>bold</b></span></p>
    </div>


    --
    Rob
    RobG, Aug 18, 2006
    #2
    1. Advertising

  3. Guest

    Thank you for your reply. I see that srcElement is the property I
    want, but how do I get your function to return the NAME or ID of the
    element (checkbox, radio etc) that I clicked on? I tried tgt.Name but
    that doesn't work.

    Or better still, where can I find on the web a list of properties I can
    read from srcElement? I can see nodeName is one - how many others are
    there?
    , Aug 18, 2006
    #3
  4. Guest

    It's okay - I figured it out. I should have used all lower case for
    the element's property, so "event.srcElement.name" will give me the
    name, "event.srcElement.value" the value, etc.

    Thanks for your help. This information will help me a lot, and save
    loads of time when doing the validation for forms.
    , Aug 18, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joby
    Replies:
    0
    Views:
    1,627
  2. Kevin Audleman
    Replies:
    2
    Views:
    806
    Kevin Audleman
    Apr 3, 2007
  3. HANM
    Replies:
    2
    Views:
    706
    Joseph Kesselman
    Jan 29, 2008
  4. Liming
    Replies:
    3
    Views:
    137
  5. DoomedLung
    Replies:
    1
    Views:
    93
    web.dev
    Sep 28, 2006
Loading...

Share This Page