|
428 | 428 |
|
429 | 429 |
|
430 | 430 | /*
|
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). |
436 | 435 | • In order to update the DOM, you need to 'listen' for specific events happening.
|
437 | 436 | • 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. |
442 | 440 | • So an EVENT LISTENER has 2 parts:
|
443 | 441 | 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 | +} |
456 | 457 |
|
457 |
| - function setUpEventHandlers3() { |
458 |
| - handleClicks(); |
459 |
| - } |
| 458 | +function setUpEventHandlers() { |
| 459 | + handleClicks(); |
| 460 | +} |
460 | 461 |
|
461 |
| - function initialize3() { |
462 |
| - setUpEventHandlers3(); |
463 |
| - } |
| 462 | +function initialize() { |
| 463 | + setUpEventHandlers(); |
| 464 | +} |
464 | 465 |
|
465 |
| - $(initialize3); |
| 466 | +$(initialize); |
466 | 467 |
|
467 | 468 |
|
468 | 469 | /*
|
|
0 commit comments