Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Passing back an element that's been OnClicked

Reply
Thread Tools

Passing back an element that's been OnClicked

 
 
stevewy@hotmail.com
Guest
Posts: n/a
 
      08-17-2006
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?

 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      08-17-2006

http://www.velocityreviews.com/forums/(E-Mail Removed) 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

 
Reply With Quote
 
 
 
 
stevewy@hotmail.com
Guest
Posts: n/a
 
      08-18-2006
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?

 
Reply With Quote
 
stevewy@hotmail.com
Guest
Posts: n/a
 
      08-18-2006

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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to Update/insert an xml element's text----> (<element>text</element>) HANM XML 2 01-29-2008 03:31 PM
Ensuring at least one form element has been used DoomedLung Javascript 1 09-28-2006 04:43 PM
The printing has been stopped and this job has been add to the queu? dejola Computer Support 6 12-30-2005 03:26 AM
window.onload() set element value that has not been loaded? Liming Javascript 3 08-26-2005 08:49 AM
I need help I has been over 6 months since I've been able to do the system check for updates Marc Computer Support 8 07-25-2005 07:04 PM



Advertisments