Working with the Click Event
Practice using JQuery click event handling.
Lab 1: Using on, click and preventDefault methods.

Setup Instructions
Copy all the files from the start-01 folder to the practice folder and replace if necessary.
The lab's completed files can be found in the completed folder.
Steps:
-
Open the link-click-die-roll.html file in your web browser.

Use the Element Inspector window to identify the best way to select the outer element for the "Roll Again" text and the "Dice roll: Nothing here but us chickens." text.
The
#roll-linkselector can be used for the "Roll Again" text.The
#roll-valueselector can be used for the "Dice roll: Nothing here but us chickens." text.
-
DIY: Open the link-click-die-roll.js file in your code editor and replace the following line …
console.log(getDiceRoll());
… with JQuery to update the
roll-valueelement with the dice roll value. Save and test.function updateView(){console.log(getDiceRoll());$('#roll-value').html(getDiceRoll()); } -
In the link-click-die-roll.js add after the following line …
console.log("DOM is ready!");… the following …
$("#roll-link").click (function(){ updateView(); });… and save and test.
The dice roll link is active and updates the web page when clicked.
-
Does the hyperlink change the web browser history? How can you verify?
The anchor element …
<a id="roll-link" href="#">Roll Again</a>
… generates URL and initiates an HTTP request to the web browser. That URL added to the web browser history.
To verify you can view the history detail and see a hyperlink ending with the # hash symbol. You might start by first reloading the page without the trailing hash symbol. Then click on the hyperlink. At this point you may even see a URL loading on your web page, but if you look in the history you should see two entries, one with and one without the hash symbol.
This is a small web page in height. The visual effects may not be noticable. Generally the hash symbol for the
hrefattribute would scroll the document to the top of the page. Some authors use#!where the!is a bogus named anchor element to avoid the scrolling. However an entry into the web browser history may still occur. -
DIY: In the link-click-die-roll.js file change the
clickmethod to prevent the default behavior of the hyperlink. Test your solution.For events that have a default behavior you want to override, you use the
preventDefaultmethod int theeventobject that JQuery passes to its event methods.$("#roll-link").click(function(){ updateView(); event.preventDefault(); }); -
DIY: In the link-click-die-roll.js file convert the anonymous function used in the
clickmethod to a function namedrollClickHandlerand test.$("#roll-link").click(rollClickHandler); function rollClickHandler(event){ updateView(); event.preventDefault(); } -
DIY: In the link-click-die-roll.js file replace the JQuery
clickmethod with the JQueryonand test.$("#roll-link").click(rollClickHandler);$("#roll-link").on("click", rollClickHandler); -
DIY: In the link-click-die-roll.html file replace the
aelement with thebuttonelement. Make necessary changes to the link-click-die-roll.js code.FYI: The link-click-die-roll.css file has styling for all
buttonelements.
Changes to the link-click-die-roll.html file.
<a id="roll-link" href="#">Roll Again</a><button id="roll-link">Roll Again</button>Changes to the link-click-die-roll.js file.
function rollClickHandler(event){ updateView();event.preventDefault();}The
buttonelement does not have default behavior effecting the web browser so thepreventDefaultmethod is not required.Optionally the
eventparameter tot herollClickHandleris no longer used and could be removed.
Lab 2: Using the this with a class selector click event handler.
Explores the JQuery event currentTarget object.

Setup Instructions
Copy all the files from the start-02 folder to the practice folder and replace if necessary.
The lab's completed files can be found in the completed folder.
Steps:
-
Open the class-stopwatch.html file in your web browser. Use the web browser tools to examine how the elapsed time is being updated by JQuery.
Line 6 of the class-stopwatch.js file creates a JQuery object for the
#counter-valueelement and uses thehtmlmethod to replace its contents with thesimpleStopWatch.toHHMMSSmethod return value.Screen shots taken at 10 seconds of elapsed time to illustrate.


-
Before the
updateView()function create a click handler for the stopwatch controls using a JQuery object variable named$stopwatchControlsand the JQueryonmethod. Name the handler functionstopwatchHandler. Then test in the web browser tools.

var $stopwatchControls = $(".stopwatch-control"); $stopwatchControls.on("click", stopwatchControlHandler); function stopwatchControlHandler(e){ console.log('stopwatchControlHandler'); e.preventDefault(); }
Line 5 creates a variable representing the JQuery instance object
$stopwatchControlsfor the class.stopwatch-control.Line 6 registers the
stopwatchControlHandlerfunction the click event for the$stopwatchControlsJQuery object.Lines 7-10 are the
stopwatchControlHandlerfunction. The JQuery event object is added the function signature as the argument variable namede.Because all the controls are the
aelement, thepreventDefaultmethod for the JQuery event object is included on line 9. -
In the class-stopwatch.js file add the following lines in the
stopwatchControlHandlerfunction …console.log(this); console.log(e.currentTarget); console.log(this === e.currentTarget);
… then save and test for all controls.
Describe the results.
The
thisande.currentTargetobjects reference the same.stopwatch-controlHTMLElement that was clicked. The following illustrates the results:
-
In the class-stopwatch.js file add the following lines in the
stopwatchControlHandlerfunction …console.log($stopwatchControls); console.log($stopwatchControls[0]); console.log($stopwatchControls[0] === this); console.log($stopwatchControls[1]); console.log($stopwatchControls[1] === this); console.log($stopwatchControls[2]); console.log($stopwatchControls[2] === this);
… then save and test for all controls.
Describe the results.
$stopwatchControls[0],$stopwatchControls[1]and$stopwatchControls[2]are HTML elements selected for the$stopwatchControlsJQuery instance object.The
$stopwatchControls[0]HTML element is equal to thethisobject when the start link is clicked.![Sources and Console views examining the this and $stopwatchcontrols[0] objects. Sources and Console views examining the this and $stopwatchcontrols[0] objects.](https://uploads.teachablecdn.com/attachments/eLfV9pQzRQucsiTJg8Qt_stopwatch-examine-stopwatchcontrols-0.png)
The
$stopwatchControls[1]HTML element is equal to thethisobject when the stop link is clicked.![Sources and Console views examining the this and $stopwatchcontrols[1] objects. Sources and Console views examining the this and $stopwatchcontrols[1] objects.](https://uploads.teachablecdn.com/attachments/50pK1FEGRg1eFG4VMQfv_stopwatch-examine-stopwatchcontrols-1.png)
The
$stopwatchControls[2]HTML element is equal to thethisobject when the reset link is clicked.![Sources and Console views examining the this and $stopwatchcontrols[2] objects. Sources and Console views examining the this and $stopwatchcontrols[2] objects.](https://uploads.teachablecdn.com/attachments/dN0joyAOSdmtoNAcNYk0_stopwatch-examine-stopwatchcontrols-2.png)
-
Change the class-stopwatch.js code so the
stopwatchobject is not automatically started.Then in
stopwatchControlHandlerfunction add a switch statement that tests for thethisobject and uses thesimpleStopwatchobject'scontinue,pauseandresetmethods respectively for the start, stop and reset links. Save, test.Remove all
console.logstatements once your are done.Remove the following line.
simpleStopwatch.addUpdateListener(updateView);
simpleStopwatch.continue();var $stopwatchControls = $(".stopwatch-control");A possible
switchstatement.function stopwatchControlHandler(e){console.log('stopwatchControlHandler');console.log(this);console.log(e.currentTarget);console.log(this === e.currentTarget);console.log($stopwatchControls);console.log($stopwatchControls[0]);console.log($stopwatchControls[0] === this);console.log($stopwatchControls[1]);console.log($stopwatchControls[1] === this);console.log($stopwatchControls[2]);console.log($stopwatchControls[2] === this);switch(this){ case $stopwatchControls[0]: simpleStopwatch.continue(); break; case $stopwatchControls[1]: simpleStopwatch.pause(); break; default: simpleStopwatch.reset(); }The
thisobject is one of the thre HTML elements that can trigger the JQuery$stopWatchControlsobject'sclickevent. Each of the HTML elements are represented in the$stopWatchControlsobject as the 0, 1, and 2 properties.
