Javascript Course

The this and target keywords 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 object. event.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(ev){
 // here you can use: ev.target
}

The this keyword and ev.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 ev.target.
<!doctype html>
<html>
<head>
<title>title</title>
<style>
#parent {
 height: 7em;
 background: #00da01;
 padding: 1em;
}
#child {
 height: 70%;
 margin: 1em 5em;
 background: #8889fe;
}
</style>
</head>
<body>
<h4>example this and target</h4>
<p>Click on the Child (blue rectangle), then on the Parent (green area), and see the difference in the alert window.</p>

<div id='parent'>Parent
 <div id='child'>Child</div>
</div>
<script>
document.getElementById('parent').onclick = function(ev){
 var id1 = this.id;
 var id2 = ev.target.id;
 alert('this = '+ id1 +'\n ev.target = '+ id2);
}
</script>
</body>
</html>

Working with this keyword and event.target

The this keyword and ev.target are useful in events 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 ev.target).


- In the following example the alert window is displayed only when you click on the Parent DIV.
<!doctype html>
<html>
<head>
<title>title</title>
<style>
#parent {
 height: 7em;
 background: #00da01;
 padding: 1em;
}
#child {
 height: 70%;
 margin: 1em 5em;
 background: #8889fe;
}
</style>
</head>
<body>
<h4>example ev.target</h4>
<p>Click on the Child (blue rectangle), then on the Parent (green area).</p>

<div id='parent'>Parent
 <div id='child'>Child</div>
</div>

<script>
document.getElementById('parent').onclick = function(ev){
 if(this === ev.target) {
 alert(this.id);
 }
}
</script>
</body>
</html>

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
this and target in JavaScript Events

Last accessed pages

  1. PHP getElementById and getElementsByTagName (49443)
  2. Area and Perimeter Calculator for 2D shapes (10161)
  3. PHP MySQL - SELECT, ORDER BY (12815)
  4. Node.js Move and Copy Directory (20115)
  5. Pencil and Pen Tools (2254)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (477)
  2. CSS cursor property - Custom Cursors (81)
  3. The Mastery of Love (71)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (63)
  5. CSS3 2D transforms (46)