Connecting Tech Pros Worldwide Help | Site Map

Passing back an element that's been OnClicked

  #1  
Old August 17th, 2006, 04:55 PM
stevewy@hotmail.com
Guest
 
Posts: n/a
If I surround a number of form elements with a <DIVtag 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?

  #2  
Old August 18th, 2006, 12:25 AM
RobG
Guest
 
Posts: n/a

re: Passing back an element that's been OnClicked



stevewy@hotmail.com wrote:
Quote:
If I surround a number of form elements with a <DIVtag 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

  #3  
Old August 18th, 2006, 11:05 AM
stevewy@hotmail.com
Guest
 
Posts: n/a

re: Passing back an element that's been OnClicked


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?

  #4  
Old August 18th, 2006, 01:35 PM
stevewy@hotmail.com
Guest
 
Posts: n/a

re: Passing back an element that's been OnClicked



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.

Closed Thread