Passing back an element that's been OnClicked

S

stevewy

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?
 
R

RobG

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>
 
S

stevewy

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?
 
S

stevewy

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.
 

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
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top