{"id":140,"date":"2020-08-01T21:07:19","date_gmt":"2020-08-02T01:07:19","guid":{"rendered":"https:\/\/pressbooks.library.ryerson.ca\/webdesignlab\/chapter\/tutorial-8-%e2%80%a2-javascript-and-jquery\/"},"modified":"2021-07-26T10:31:18","modified_gmt":"2021-07-26T14:31:18","slug":"tutorial-7-javascript-and-jquery","status":"publish","type":"chapter","link":"https:\/\/pressbooks.library.torontomu.ca\/webdesignlab\/chapter\/tutorial-7-javascript-and-jquery\/","title":{"raw":"Tutorial 7 \u2022 JavaScript and jQuery","rendered":"Tutorial 7 \u2022 JavaScript and jQuery"},"content":{"raw":"This tutorial demonstrates the basics of using the JavaScript programming language and jQuery library to add interactivity to a web site. Thanks to Ahmed (Am) Sagarwala (GCM '08) for contributing the section on jQuery.\r\n<h1>\u201cPlain\u201d JavaScript<\/h1>\r\n<h2>Using a Variable, Conditional Statement, and Function<\/h2>\r\nIn this section we will see how to write JavaScript code, include a conditional statement, and place the code into a function so it can be used multiple times.\r\n<ol>\r\n \t<li>Open the starter document for the \u201cFireside Chat.\u201d<\/li>\r\n \t<li>Inside the &lt;div id=\"livearea\"&gt;, place opening and closing &lt;script&gt; tags and add the code:\r\n<pre>document.write(\"Welcome to the Fireside Chat web page.\");<\/pre>\r\nNote that the text is written below the heading.<\/li>\r\n \t<li>Add opening and closing &lt;p&gt; tags inside the quotes. Add a style statement and style the text as you like.<\/li>\r\n \t<li>Above the \"document.write\" line, add a variable statement to hold the reader's name:\r\n<pre>var name=prompt(\"Please type your name.\");<\/pre>\r\n<\/li>\r\n \t<li>Add the variable name to the statement by placing it outside the quotes and connecting it to the text with \u201c+\u201d signs:\r\n<pre>document.write(\"&lt;p style=\"font-family: Georgia;\"&gt;Welcome to the Fireside Chat web page, \" + name + \".&lt;\/p&gt;\");<\/pre>\r\n<\/li>\r\n \t<li>If the reader does not type a name, the statement will print with a blank. To prevent the statement from printing if the user does not enter a name, place the \u201cdocument.write\u201d statement in a conditional \u201cif\u201d statement:\r\n<pre>if (name!=\"\") {\r\n\r\ndocument.write(\"p style=\"font-family: Georgia;\"&gt;Welcome to the Fireside Chat web page, \" + name + \".&lt;\/p&gt;\");\r\n\r\n}<\/pre>\r\n<\/li>\r\n \t<li>To make a function out of the statements, add opening and closing &lt;script&gt; tags to the &lt;head&gt;. Create the function by writing:\r\n<pre>function writeName() {\r\n\r\nplace code here\r\n\r\n}<\/pre>\r\n<\/li>\r\n \t<li>Call the function by keeping the &lt;script&gt; tags in the &lt;body&gt; and writing:\r\n<pre>writeName();<\/pre>\r\n<\/li>\r\n<\/ol>\r\n<h2>Making a Print Button<\/h2>\r\nIn this section we will create a function to print the page and link it to a button. Adding an \u201c@media\u201d query will enable fitting the document on a letter-size page and omitting the buttons from the print.\r\n<ol>\r\n \t<li>Program the button at the bottom of the page to print the page.<\/li>\r\n \t<li>Add a function to the &lt;script&gt; tag in the &lt;head&gt;:\r\n<pre>function printPage() {\r\n\r\nthis.window.print();\r\n\r\n}<\/pre>\r\n<\/li>\r\n \t<li>Call the printPage function by linking it to the \u201cPrint\u201d button:\r\n<pre>&lt;button onClick=\"printPage()\"&gt;Print&lt;\/button&gt;<\/pre>\r\n<\/li>\r\n \t<li>Add an @media print {} query to confine the page to 8.5x11\" and hide the buttons in the print view:\r\n<pre>@media print {\r\n\r\nbody {\r\n\r\ntransform: scale(0.85);\r\n\r\n} #page {\r\n\r\nwidth: 8.25in;\r\n\r\nheight: 10.0in;\r\n\r\n} footer {\r\n\r\ndisplay: none;\r\n\r\n}<\/pre>\r\n<\/li>\r\n<\/ol>\r\n<h2>Using Buttons to Change Color<\/h2>\r\nIn this section we will link a button to a function to change color of the text.\r\n<ol>\r\n \t<li>In the &lt;script&gt; tag in the head, add a function to change color of the div \u201clivearea\u201d:\r\n<pre>function colorGrey() {\r\n\r\ndocument.getElementById(\"livearea\").style.color=\"dimgray\";\r\n\r\n}<\/pre>\r\n<\/li>\r\n \t<li>Link the function to the \u201cGrey\u201d button:\r\n<pre>&lt;button onClick=\"colorGrey()\"&gt;Grey&lt;\/button&gt;<\/pre>\r\n<\/li>\r\n \t<li>Create a similar function to change the text color back to black.<\/li>\r\n<\/ol>\r\n<h1>jQuery JavaScript Library<\/h1>\r\njQuery is a curated library of functions that was started by a group of computer science professors at MIT. jQuery simplifies the application of JavaScript by providing pre-coded functions. Instead of scripting all of the steps to select an element and make it do something, we can usually achieve this in a few lines of code using jQuery\u2019s functions.\r\n\r\nThe jQuery library is only a few K in file size and can be downloaded and hosted on your site or linked-to on the code.jquery.com site.\r\n\r\nTo create an interaction using jQuery, think of a sequence of events\u2014an action and the resulting reaction. For example:\r\n<ul>\r\n \t<li>The user moves the cursor to a button.<\/li>\r\n \t<li>A click action on the button takes place.<\/li>\r\n \t<li>An element on the page reacts by fading out.<\/li>\r\n<\/ul>\r\n[caption id=\"\" align=\"aligncenter\" width=\"712\"]<img src=\"http:\/\/pressbooks.library.ryerson.ca\/webdesignlab\/wp-content\/uploads\/sites\/133\/2020\/08\/image1-4.png\" width=\"712\" height=\"auto\" alt=\"image\" \/> jQuery exercise[\/caption]\r\n<h2><a><\/a>1. Create Custom Style to Change Color<\/h2>\r\nCreate a custom style that will be applied to change the color.\r\n<pre>#box.new-color {\r\nbackground: lightblue;\r\n}<\/pre>\r\n<h2><a><\/a>2. Link to jQuery<\/h2>\r\nLinks to the jQuery and jQueryUI libraries need to be inserted. You can download both libraries and host them on your site, or link to them on Google\u2019s hosted libraries site, GoogleAPIs (application programmer interfaces). Place the jQuery link anywhere before your JavaScript code, but it is best to place this before the end of your &lt;\/body&gt; tag. This helps with page load times and there improves user experience. Around line 49 in our tutorial is ideal for this (before the line of code starting with $).\r\n<pre>&lt;script src=\"https:\/\/code.jquery.com\/jquery-3.3.1.min.js\"&gt;&lt;\/script&gt;\r\n\r\n&lt;script src=\"https:\/\/code.jquery.com\/ui\/1.12.0\/jquery-ui.min.js\"&gt;&lt;\/script&gt;\r\n\r\n3. Add Selectors to Code<\/pre>\r\n<a href=\"http:\/\/pressbooks.library.ryerson.ca\/webdesignlab\/wp-content\/uploads\/sites\/133\/2020\/08\/jQuery-selectors.jpg\"><img src=\"http:\/\/pressbooks.library.ryerson.ca\/webdesignlab\/wp-content\/uploads\/sites\/133\/2020\/08\/jQuery-selectors.jpg\" alt=\"Visual demonstration of the code. When the button is triggered, the box will change color.\" width=\"540\" height=\"288\" class=\"alignright wp-image-196 size-full\" \/><\/a>Add your selectors to the JavaScript code:\r\n<pre>$('#button-change').click(function() {\r\n\r\n$(\"#box-2\").toggleClass(\"new-color\", 1000);\r\n\r\n});<\/pre>\r\n<h2>4. fadeToggle<\/h2>\r\nUse the fadeToggle function to fade the box in and out.\r\n<pre>$('#fade').click(function() {\r\n$(\"#box-1\").fadeToggle(1000);\r\n});<\/pre>\r\n<h2>5. slideToggle<\/h2>\r\nUse the slideToggle function to make the boxes slide up and down.\r\n<pre>$('#slide').click(function() {\r\n$(\"#box-3, #box-4, #box-5\").slideToggle(1000);\r\n});<\/pre>\r\n<h2>6. animate<\/h2>\r\nUse the animate function to move Box 6 down and up with the respective buttons. Animation can be done in the directions top, bottom, left, and right. The operators \"+=\" and \"-=\" mean add or subtract the specified value (215px) to the current value.\r\n<pre>$('#down).click(function() {\r\n$(\"#box-6\").animate({\r\ntop: '+=215px'}, 1000);\r\n});\r\n\r\n$('#up).click(function() {\r\n$(\"#box-6\").animate({\r\ntop: '-=215px'}, 1000);\r\n});<\/pre>\r\n<h2>7. draggable<\/h2>\r\nUse the draggable function to enable readers to click-and-drag Box 7 around the page.\r\n<pre>$('#box-7').draggable();<\/pre>\r\n&nbsp;","rendered":"<p>This tutorial demonstrates the basics of using the JavaScript programming language and jQuery library to add interactivity to a web site. Thanks to Ahmed (Am) Sagarwala (GCM &#8217;08) for contributing the section on jQuery.<\/p>\n<h1>\u201cPlain\u201d JavaScript<\/h1>\n<h2>Using a Variable, Conditional Statement, and Function<\/h2>\n<p>In this section we will see how to write JavaScript code, include a conditional statement, and place the code into a function so it can be used multiple times.<\/p>\n<ol>\n<li>Open the starter document for the \u201cFireside Chat.\u201d<\/li>\n<li>Inside the &lt;div id=&#8221;livearea&#8221;&gt;, place opening and closing &lt;script&gt; tags and add the code:\n<pre>document.write(\"Welcome to the Fireside Chat web page.\");<\/pre>\n<p>Note that the text is written below the heading.<\/li>\n<li>Add opening and closing &lt;p&gt; tags inside the quotes. Add a style statement and style the text as you like.<\/li>\n<li>Above the &#8220;document.write&#8221; line, add a variable statement to hold the reader&#8217;s name:\n<pre>var name=prompt(\"Please type your name.\");<\/pre>\n<\/li>\n<li>Add the variable name to the statement by placing it outside the quotes and connecting it to the text with \u201c+\u201d signs:\n<pre>document.write(\"&lt;p style=\"font-family: Georgia;\"&gt;Welcome to the Fireside Chat web page, \" + name + \".&lt;\/p&gt;\");<\/pre>\n<\/li>\n<li>If the reader does not type a name, the statement will print with a blank. To prevent the statement from printing if the user does not enter a name, place the \u201cdocument.write\u201d statement in a conditional \u201cif\u201d statement:\n<pre>if (name!=\"\") {\r\n\r\ndocument.write(\"p style=\"font-family: Georgia;\"&gt;Welcome to the Fireside Chat web page, \" + name + \".&lt;\/p&gt;\");\r\n\r\n}<\/pre>\n<\/li>\n<li>To make a function out of the statements, add opening and closing &lt;script&gt; tags to the &lt;head&gt;. Create the function by writing:\n<pre>function writeName() {\r\n\r\nplace code here\r\n\r\n}<\/pre>\n<\/li>\n<li>Call the function by keeping the &lt;script&gt; tags in the &lt;body&gt; and writing:\n<pre>writeName();<\/pre>\n<\/li>\n<\/ol>\n<h2>Making a Print Button<\/h2>\n<p>In this section we will create a function to print the page and link it to a button. Adding an \u201c@media\u201d query will enable fitting the document on a letter-size page and omitting the buttons from the print.<\/p>\n<ol>\n<li>Program the button at the bottom of the page to print the page.<\/li>\n<li>Add a function to the &lt;script&gt; tag in the &lt;head&gt;:\n<pre>function printPage() {\r\n\r\nthis.window.print();\r\n\r\n}<\/pre>\n<\/li>\n<li>Call the printPage function by linking it to the \u201cPrint\u201d button:\n<pre>&lt;button onClick=\"printPage()\"&gt;Print&lt;\/button&gt;<\/pre>\n<\/li>\n<li>Add an @media print {} query to confine the page to 8.5&#215;11&#8243; and hide the buttons in the print view:\n<pre>@media print {\r\n\r\nbody {\r\n\r\ntransform: scale(0.85);\r\n\r\n} #page {\r\n\r\nwidth: 8.25in;\r\n\r\nheight: 10.0in;\r\n\r\n} footer {\r\n\r\ndisplay: none;\r\n\r\n}<\/pre>\n<\/li>\n<\/ol>\n<h2>Using Buttons to Change Color<\/h2>\n<p>In this section we will link a button to a function to change color of the text.<\/p>\n<ol>\n<li>In the &lt;script&gt; tag in the head, add a function to change color of the div \u201clivearea\u201d:\n<pre>function colorGrey() {\r\n\r\ndocument.getElementById(\"livearea\").style.color=\"dimgray\";\r\n\r\n}<\/pre>\n<\/li>\n<li>Link the function to the \u201cGrey\u201d button:\n<pre>&lt;button onClick=\"colorGrey()\"&gt;Grey&lt;\/button&gt;<\/pre>\n<\/li>\n<li>Create a similar function to change the text color back to black.<\/li>\n<\/ol>\n<h1>jQuery JavaScript Library<\/h1>\n<p>jQuery is a curated library of functions that was started by a group of computer science professors at MIT. jQuery simplifies the application of JavaScript by providing pre-coded functions. Instead of scripting all of the steps to select an element and make it do something, we can usually achieve this in a few lines of code using jQuery\u2019s functions.<\/p>\n<p>The jQuery library is only a few K in file size and can be downloaded and hosted on your site or linked-to on the code.jquery.com site.<\/p>\n<p>To create an interaction using jQuery, think of a sequence of events\u2014an action and the resulting reaction. For example:<\/p>\n<ul>\n<li>The user moves the cursor to a button.<\/li>\n<li>A click action on the button takes place.<\/li>\n<li>An element on the page reacts by fading out.<\/li>\n<\/ul>\n<figure style=\"width: 712px\" class=\"wp-caption aligncenter\"><img decoding=\"async\" src=\"http:\/\/pressbooks.library.ryerson.ca\/webdesignlab\/wp-content\/uploads\/sites\/133\/2020\/08\/image1-4.png\" width=\"712\" height=\"auto\" alt=\"image\" \/><figcaption class=\"wp-caption-text\">jQuery exercise<\/figcaption><\/figure>\n<h2><a><\/a>1. Create Custom Style to Change Color<\/h2>\n<p>Create a custom style that will be applied to change the color.<\/p>\n<pre>#box.new-color {\r\nbackground: lightblue;\r\n}<\/pre>\n<h2><a><\/a>2. Link to jQuery<\/h2>\n<p>Links to the jQuery and jQueryUI libraries need to be inserted. You can download both libraries and host them on your site, or link to them on Google\u2019s hosted libraries site, GoogleAPIs (application programmer interfaces). Place the jQuery link anywhere before your JavaScript code, but it is best to place this before the end of your &lt;\/body&gt; tag. This helps with page load times and there improves user experience. Around line 49 in our tutorial is ideal for this (before the line of code starting with $).<\/p>\n<pre>&lt;script src=\"https:\/\/code.jquery.com\/jquery-3.3.1.min.js\"&gt;&lt;\/script&gt;\r\n\r\n&lt;script src=\"https:\/\/code.jquery.com\/ui\/1.12.0\/jquery-ui.min.js\"&gt;&lt;\/script&gt;\r\n\r\n3. Add Selectors to Code<\/pre>\n<p><a href=\"http:\/\/pressbooks.library.ryerson.ca\/webdesignlab\/wp-content\/uploads\/sites\/133\/2020\/08\/jQuery-selectors.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/pressbooks.library.ryerson.ca\/webdesignlab\/wp-content\/uploads\/sites\/133\/2020\/08\/jQuery-selectors.jpg\" alt=\"Visual demonstration of the code. When the button is triggered, the box will change color.\" width=\"540\" height=\"288\" class=\"alignright wp-image-196 size-full\" srcset=\"https:\/\/pressbooks.library.torontomu.ca\/webdesignlab\/wp-content\/uploads\/sites\/133\/2020\/08\/jQuery-selectors.jpg 540w, https:\/\/pressbooks.library.torontomu.ca\/webdesignlab\/wp-content\/uploads\/sites\/133\/2020\/08\/jQuery-selectors-300x160.jpg 300w, https:\/\/pressbooks.library.torontomu.ca\/webdesignlab\/wp-content\/uploads\/sites\/133\/2020\/08\/jQuery-selectors-65x35.jpg 65w, https:\/\/pressbooks.library.torontomu.ca\/webdesignlab\/wp-content\/uploads\/sites\/133\/2020\/08\/jQuery-selectors-225x120.jpg 225w, https:\/\/pressbooks.library.torontomu.ca\/webdesignlab\/wp-content\/uploads\/sites\/133\/2020\/08\/jQuery-selectors-350x187.jpg 350w\" sizes=\"auto, (max-width: 540px) 100vw, 540px\" \/><\/a>Add your selectors to the JavaScript code:<\/p>\n<pre>$('#button-change').click(function() {\r\n\r\n$(\"#box-2\").toggleClass(\"new-color\", 1000);\r\n\r\n});<\/pre>\n<h2>4. fadeToggle<\/h2>\n<p>Use the fadeToggle function to fade the box in and out.<\/p>\n<pre>$('#fade').click(function() {\r\n$(\"#box-1\").fadeToggle(1000);\r\n});<\/pre>\n<h2>5. slideToggle<\/h2>\n<p>Use the slideToggle function to make the boxes slide up and down.<\/p>\n<pre>$('#slide').click(function() {\r\n$(\"#box-3, #box-4, #box-5\").slideToggle(1000);\r\n});<\/pre>\n<h2>6. animate<\/h2>\n<p>Use the animate function to move Box 6 down and up with the respective buttons. Animation can be done in the directions top, bottom, left, and right. The operators &#8220;+=&#8221; and &#8220;-=&#8221; mean add or subtract the specified value (215px) to the current value.<\/p>\n<pre>$('#down).click(function() {\r\n$(\"#box-6\").animate({\r\ntop: '+=215px'}, 1000);\r\n});\r\n\r\n$('#up).click(function() {\r\n$(\"#box-6\").animate({\r\ntop: '-=215px'}, 1000);\r\n});<\/pre>\n<h2>7. draggable<\/h2>\n<p>Use the draggable function to enable readers to click-and-drag Box 7 around the page.<\/p>\n<pre>$('#box-7').draggable();<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"author":6,"menu_order":7,"template":"","meta":{"pb_show_title":"on","pb_short_title":"","pb_subtitle":"","pb_authors":[],"pb_section_license":""},"chapter-type":[],"contributor":[],"license":[],"class_list":["post-140","chapter","type-chapter","status-publish","hentry"],"part":3,"_links":{"self":[{"href":"https:\/\/pressbooks.library.torontomu.ca\/webdesignlab\/wp-json\/pressbooks\/v2\/chapters\/140","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pressbooks.library.torontomu.ca\/webdesignlab\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/pressbooks.library.torontomu.ca\/webdesignlab\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/pressbooks.library.torontomu.ca\/webdesignlab\/wp-json\/wp\/v2\/users\/6"}],"version-history":[{"count":8,"href":"https:\/\/pressbooks.library.torontomu.ca\/webdesignlab\/wp-json\/pressbooks\/v2\/chapters\/140\/revisions"}],"predecessor-version":[{"id":405,"href":"https:\/\/pressbooks.library.torontomu.ca\/webdesignlab\/wp-json\/pressbooks\/v2\/chapters\/140\/revisions\/405"}],"part":[{"href":"https:\/\/pressbooks.library.torontomu.ca\/webdesignlab\/wp-json\/pressbooks\/v2\/parts\/3"}],"metadata":[{"href":"https:\/\/pressbooks.library.torontomu.ca\/webdesignlab\/wp-json\/pressbooks\/v2\/chapters\/140\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/pressbooks.library.torontomu.ca\/webdesignlab\/wp-json\/wp\/v2\/media?parent=140"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/pressbooks.library.torontomu.ca\/webdesignlab\/wp-json\/pressbooks\/v2\/chapter-type?post=140"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/pressbooks.library.torontomu.ca\/webdesignlab\/wp-json\/wp\/v2\/contributor?post=140"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/pressbooks.library.torontomu.ca\/webdesignlab\/wp-json\/wp\/v2\/license?post=140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}