The "this" and "target" can be used in JavaScript events to get the element associated to the registered event.
- "this" - is allways what the event was bound to (the object to which the event was registered).
- "target" - is a property of the event. "e.target" is the element that triggers the event.

• To can use the "target" property, you must add a parameter to the function with the event.
Syntax:
object.event = function(e) {
  // here you can use e.target
}
"this" and "e.target" make the difference between the object to which the event is registered and the element that triggers the event. Sometimes they represent the same object, but not allways, especially when we work with parent and children elements.
- Here's an example. A #parent DIV with a #child DIV inside it; and onclick event registered to #parent, getting the ID of the element returned by "this" and "e.target".
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Example this and target</title>
<style type="text/css">
#parent {
 height: 7em;
 background: #00da01;
 padding: 1em;
}
#child {
 height: 80%;
 margin: 0.5em 1.5em;
 background: #8889fe;
}
</style>
</head>
<body>

<div id="parent">Parent
 <div id="child">Child</div>
</div>
<script type="text/javascript">
document.getElementById('parent').onclick = function(e) {
  var id1 = this.id;
  var id2 = e.target.id;
  alert('this = '+ id1 +'\n e.target = '+ id2);
}
</script>

</body>
</html>
Demo, click on the Child (blue rectangle), then on the Parent (green area), and see the difference in the alert window.
Parent
Child
• Working with "this" and "e.target" in events is useful for example when you want to register an event with instructions to an element but not to its children, or with different instructions for children. Or, when you want to work exactly with the element which triggers the event (use "e.target").
- In the following example the alert window is displayed only when you click on the Parent DIV.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Example this and target</title>
<style type="text/css">
#parent {
 height: 7em;
 background: #00da01;
 padding: 1em;
}
#child {
 height: 80%;
 margin: 0.5em 1.5em;
 background: #8889fe;
}
</style>
</head>
<body>

<div id="parent">Parent
 <div id="child">Child</div>
</div>
<script type="text/javascript">
document.getElementById('parent').onclick = function(e) {
  if(this === e.target) {
    alert(this.id);
  }
}
</script>

</body>
</html>
Demo, click on the Child (blue rectangle), then on the Parent (green area).
Parent
Child

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used to add definition lists into a <dl> element?
<dt> <dd> <li>
<dl>
 <dt>HTML</dt>
  <dd> - Hyper Text Markup Language</dd>
  <dd> - Language for web pages</dd>
</dl>
Which CSS property can hide an element on page, letting an empty space in its place?
display position visibility
#id {
  visibility: hidden;
}
Click on the event which is triggered when the mouse clicks on an object.
onclick onmouseover onfocus
document.getElementById("id").onclick = function(){
  alert("http://CoursesWeb.net/");
}
Indicate the PHP variable that contains the contents of both $_GET, $_POST, and $_COOKIE arrays.
$_SESSION $_GET $_REQUEST
if(isset($_REQUEST["id"])) {
  echo $_REQUEST["id"];
}
this and target in JavaScript Events

Last accessed pages

  1. HTTP Errors and Logging (39)
  2. Add, Change, and Remove Attributes with jQuery (35999)
  3. Select in MySQL, Output results in HTML Table (3789)
  4. jQuery Drag and Drop Rows between two similar Tables (6875)
  5. PhpSpreadsheet - Read, Write Excel and LibreOffice Calc files (8371)

Popular pages this month

  1. Read Excel file data in PHP - PhpExcelReader (443)
  2. PhpSpreadsheet - Read, Write Excel and LibreOffice Calc files (351)
  3. Register and show online users and visitors (319)
  4. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (300)
  5. Insert, Select and Update NULL value in MySQL (214)