Skip to content

Commit 1f127ba

Browse files
committed
updated 7
1 parent cb049c5 commit 1f127ba

File tree

1 file changed

+29
-28
lines changed

1 file changed

+29
-28
lines changed

JavaScript Libraries/jQuery/jQuery.js

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -428,41 +428,42 @@
428428

429429

430430
/*
431-
7. What is an Event Listener?
432-
/////////////////////////////
433-
• An Event Listener listens for a specific event to happen (i.e. submit, click, etc.) and does something (i.e. callback function).
434-
435-
• To take advantage of DOM manipulation, you need to be able to alter the DOM when EVENTS happen.
431+
7. How do you use event listeners with jQuery?
432+
//////////////////////////////////////////////
433+
• An Event Listener listens for a specific event to happen (i.e. submit, click, etc.) and does
434+
something (i.e. callback function).
436435
• In order to update the DOM, you need to 'listen' for specific events happening.
437436
• For example:
438-
• an app LISTENS when the user submits a form
439-
• an app LISTENS for when the user inputs a search term.
440-
• an app LISTENS for when the user clicks on an element in the page to launch an animation.
441-
437+
• An app LISTENS when the user submits a form.
438+
• An app LISTENS for when the user inputs a search term.
439+
• An app LISTENS for when the user clicks on an element in the page to launch an animation.
442440
• So an EVENT LISTENER has 2 parts:
443441
1. Specify what event to listen for.
444-
2. provide a CALLBACK FUNCTION that runs when the event occurs.
445-
*/
446-
447-
448-
function handleClicks() {
449-
let clickCount = 0; // Stores click count.
450-
$('.click-counter').text(clickCount); // show current click count.
451-
$('main').on('click', '#clicker', function(event) { // On click...
452-
clickCount += 1; // ... increment by 1...
453-
$('.click-counter').text(clickCount); // ... show new click count.
454-
});
455-
}
442+
2. provide a CALLBACK FUNCTION that runs when the event occurs.
443+
444+
• Use the ".on" method with the event and the callback to implement an event listener.
445+
• In the example below, the user listens on main (see event delegation) and, on "click"
446+
(of the only button in this study) runs a callback function that increments the counter
447+
by one and displays the updated clickCount.
448+
*/
449+
function handleClicks() {
450+
let clickCount = 0; // Stores click count.
451+
$('.click-counter').text(clickCount); // show current click count.
452+
$('main').on('click', '#clicker', function(event) { // On click...
453+
clickCount += 1; // ... increment by 1...
454+
$('.click-counter').text(clickCount); // ... show new click count.
455+
});
456+
}
456457

457-
function setUpEventHandlers3() {
458-
handleClicks();
459-
}
458+
function setUpEventHandlers() {
459+
handleClicks();
460+
}
460461

461-
function initialize3() {
462-
setUpEventHandlers3();
463-
}
462+
function initialize() {
463+
setUpEventHandlers();
464+
}
464465

465-
$(initialize3);
466+
$(initialize);
466467

467468

468469
/*

0 commit comments

Comments
 (0)