@@ -108,33 +108,41 @@ We've learned in the previous lesson how to bind events on **click** by setting
108108<a href =" #" class =" done" onclick =" alert('Click event')" >Show an alert box</a >
109109```
110110
111- With jQuery we can achieve this by applying event listeners to the document of the page, listening for the event.
111+ With jQuery we can achieve the same thing with an event listener. To listen for
112+ events anywhere on the page we attach the event listener to the document
113+ element.
112114
113115``` js
114116$ (document ).on (' click' ,' .done' , function () {
115117 alert (" Click event" );
116118});
117119```
118120
121+ There are two differences between these examples:
122+
123+ - This event listener will listen for ` click ` on all the elements with class
124+ ` done ` , i.e. one listener can listen to many elements.
125+ - jQuery's ` on() ` method is dynamic, so if we add new items to the page with
126+ class ` done ` the listener will listen to them as well.
127+
119128** Handling events**
120129
130+ To create your own event listener choose an ` event ` to listen for on the
131+ elements matching a ` selector ` . Then put the code you want to run each time the
132+ event occurs in the ` function ` .
133+
121134``` javascript
122135$ (document ).on (event , selector, function () {
123136 // code to be executed when event occurs
124137});
125138```
126139
127- Using the ` on() ` method, means that the click event will work even if we add new items to the DOM dynamically.
128-
129- > ` $(this) ` is the element that we have triggered the event from.
130-
131140##Waiting for the page to load
132141
133142``` js
134143$ (document ).ready (function () {
135- // here go all the interactions
136-
137- /* click, mouseover, change etc */
144+ // here go all the listeners
145+ // e.g. on click, mouseover, change etc
138146});
139147```
140148
0 commit comments