Jump to content
Larry Ullman's Book Forums

The Event Object - Target Property (Chapter 8: Event Handling) Pg 292

Recommended Posts

Hi, on page 292, Larry says that the target property 'points to the HTML element that triggered the event'. Does this mean that, in the following example, var target points to the same location as var username:

function checkUsername(e) {
	var target = e.target; 
var username = document.getElementById('name');
username.addEventListener('blur', checkUsername, false);

If so, is this a general rule i.e. does the target property 'point' to the same location as document methods such as getElementById()?



Link to post
Share on other sites

In that example, yes, target will point to the same DOM object as is assigned to the username variable.


However, something important to note is that if you set up an event handler for a DOM element that contains subelements, then that event will fire for any of the subelements as well.


For example, if you set up a click event handler for a div that contains p elements, then clicking on any of the p elements will cause the click event for the div to fire, and in that case, target will refer to the p element that was clicked on, not the parent div.


That make sense?

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...