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()?

 

Thanks.

Link to comment
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 comment
Share on other sites

 Share

×
×
  • Create New...