Chapter 7 Murach's JavaScript and jQuery, C7© 2012, Mike Murach & Associates, Inc.Slide 1.

download Chapter 7 Murach's JavaScript and jQuery, C7© 2012, Mike Murach & Associates, Inc.Slide 1.

If you can't read please download the document

Transcript of Chapter 7 Murach's JavaScript and jQuery, C7© 2012, Mike Murach & Associates, Inc.Slide 1.

Chapter 7

Chapter 7Murach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 1

ObjectivesMurach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 2

Objectives (continued)Murach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 3

The jQuery web site at www.jQuery.comMurach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 4

What jQuery offersMurach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 5

The user interface for the FAQs applicationMurach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 6

The JavaScript for the FAQs applicationMurach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 7

The JavaScript for the FAQs application (cont.)Murach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 8

The jQuery for the FAQs applicationMurach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 9

The FAQs application as a jQuery UI accordionMurach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 10

The HTML for a jQuery UI accordionMurach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 11

The JavaScript code for the jQuery UI accordionMurach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 12

Some typical plugin functionsMurach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 13

TermsMurach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 14

The syntax for a jQuery selectorMurach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 15

The HTML for the selected elementsMurach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 16

How to select elements by element, id, and classMurach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 17

How to select elements by relationshipMurach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 18

How to code multiple selectorsMurach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 19

The syntax for calling a jQuery methodMurach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 20

Examples that call jQuery methodsMurach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 21

The syntax for a jQuery event methodMurach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 22

How to code an event handler for the ready eventMurach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 23

An event handler for the click event of all h2 elementsMurach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 24

TermsMurach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 25

The user interface for the Email List applicationMurach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 26

The HTML for the Email List applicationMurach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 27

The HTML for the Email List application (cont.)Murach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 28

The jQuery for the Email List applicationMurach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 29

The jQuery for the Email List application (cont.)Murach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 30

Some of the most useful jQuery selectorsMurach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 31

Examples that use jQuery selectorsMurach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 32

A summary of the most useful jQuery methodsMurach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 33

Examples that use jQuery methodsMurach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 34

Some of the most useful jQuery event methodsMurach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 35

Examples that use jQuery event methodsMurach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 36

Other event methods that you should be aware ofMurach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 37

How to store an event handler in a variableMurach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 38

How to attach an event handler to an eventMurach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 39

How to remove an event handler from an eventMurach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 40

How to trigger an eventMurach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 41

The FAQs application in a browserMurach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 42

The HTML for the FAQs applicationMurach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 43

The HTML for the FAQs application (cont.)Murach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 44

The jQuery for the FAQs applicationMurach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 45

The user interface for the Image Swap applicationMurach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 46

The HTML for the Image Swap applicationMurach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 47

The CSS for the li elementsMurach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 48

The JavaScript for the Image Swap applicationMurach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 49

Three images with the middle image rolled overMurach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 50

The HTML for the Image Rollover applicationMurach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 51

The JavaScript for the Image Rollover applicationMurach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 52

Exercise 7-3: Develop a Book List applicationMurach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 53

Extra 7-1: Develop an Expand/Collapse appMurach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 54

Extra 7-2: Develop an Image Gallery applicationMurach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 55

Short 7-1: Modify the Future Value applicationMurach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 56

Short 7-2: Create a FAQS Rollover applicationMurach's JavaScript and jQuery, C7 2012, Mike Murach & Associates, Inc.Slide 57

Get off to a fast start with jQueryAppliedUse jQuery to develop common DOM scripting applications like the Email List, FAQs, Image Swap, and Image Rollover applications that are presented in this chapter.Knowledge1. Describe jQuery.Describe two ways to include the jQuery library in your web pages.In general terms, describe the use of jQuery selectors, methods, and event methods.Describe the syntax for a jQuery selector.Describe the use of these methods: val, next, prev, text, attr, css, addClass, removeClass, toggleClass, hide, show, and each.Describe object chaining.Describe the use of these jQuery event methods: ready, click, toggle, mouseover, and hover.Describe the use of the this keyword within a function for an event method.Dozens of methods that make it easier to add JavaScript features to your web pagesMethods that are tested for cross-browser compatibilityHow to include the jQuery file from your computer

How to include the jQuery file from a Content Delivery Network (CDN)

var $ = function (id) { return document.getElementById(id);}window.onload = function () { var faqs = $("faqs"); var h2Elements = faqs.getElementsByTagName("h2"); var h2Node; for (var i = 0; i < h2Elements.length; i++ ) { h2Node = h2Elements[i]; // Attach event handler h2Node.onclick = function () { var h2 = this; // h2 is the current h2Node object if (h2.getAttribute("class") == "plus") { h2.setAttribute("class", "minus"); } else { h2.setAttribute("class", "plus"); } if (h2.nextElementSibling.getAttribute("class") == "closed") { h2.nextElementSibling.setAttribute("class", "open"); } else { h2.nextElementSibling.setAttribute("class", "closed"); } } }}$(document).ready(function() { $("#faqs h2").toggle( function() { $(this).addClass("minus"); $(this).next().show(); }, function() { $(this).removeClass("minus"); $(this).next().hide(); } ); // end toggle}); // end ready

What is jQuery? Why is jQuery becoming so popular? Which is harder to learn: jQuery or JavaScript?>

$(document).ready(function() { $("#accordion").accordion(); });

Data validationSlide showsCarouselsjQueryContent Delivery System (CDN)jQueryUIplugin$("selector")

jQuery FAQs What is jQuery? jQuery is a library of the JavaScript functions that you're most likely to need as you develop web sites. Why is jQuery becoming so popular? Three reasons:

  • It's free.
  • It lets you get more done in less time.
  • All of its functions cross-browser compatible.

By element type: All elements in the entire document$("p")By id: The element with faqs as its id$("#faqs")By class: All elements with plus as a class$(".plus")Descendants: All elements that are descendants of the section element$("#faqs p");Adjacent siblings: All div elements that are adjacent siblings of h2 elements$("h2 + div")General siblings: All elements that are siblings of ul elements$("ul ~ p")Children: All ul elements that are children of div elements$("div > ul")$("#faqs li, div p")$("p + ul, div ~ p")$("selector").methodName(parameters)Some common jQuery methodsval()val(value)text()text(value)next([type])submit()focus()How to get the value from a text boxvar gallons = $("#gallons").val();How to set the value for an input element$("#gallons").val("");How to set the text in an element$("#email_address_error").text( "Email address is required");How to set the text for the next sibling with object chaining$("#last_name").next().text("Last name is required");How to submit a form$("#join_list").submit();How to move the focus to a form control or link$("#email_address").focus();$(selector).eventMethodName(function() { // the statements of the event handler});Two common jQuery event methodsEvent methodDescriptionready(handler)The event handler runs when the DOM is ready.click(handler)The event handler runs when the selected element is clicked.The long way$(document).ready(function() { alert("The DOM is ready");});The short way$(function(){ // (document).ready is assumed alert("The DOM is ready");});$("h2").click(function() { alert("This heading has been clicked");});The click event handler within the ready event handler$(document).ready(function() { $("h2").click(function() { alert("This heading has been clicked"); }); // end of click event handler}); // end of ready event handlerselectormethodobject chainingevent method

Join Email List

Please join our email list Email Address: *

Re-enter Email Address: *

First Name: *

$(document).ready(function() { $("#join_list").click(function() { var emailAddress1 = $("#email_address1").val(); var emailAddress2 = $("#email_address2").val(); var isValid = true;

// validate the first email address if (emailAddress1 == "") { $("#email_address1").next().text( "This field is required."); isValid = false; } else { $("#email_address1").next().text(""); } // validate the second email address if (emailAddress2 == "") { $("#email_address2").next().text( "This field is required."); isValid = false; } else if (emailAddress1 != emailAddress2) { $("#email_address2").next().text( "This entry must equal first entry."); isValid = false; } else { $("#email_address2").next().text(""); } // validate the first name entry if ($("#first_name").val() == "") { $("#first_name").next().text( "This field is required."); isValid = false; } else { $("#first_name").next().text(""); } // submit the form if all entries are valid if (isValid) { $("#email_form").submit(); } }); // end click $("#email_address1").focus();}); // end ready[attribute][attribute=value]:eq(n):even:first:first-child:gt(n):header:last:last-child:lt(n):not(selector):odd:textSelect the li elements that are the first child of their parent element$("li:first-child")Select the even tr elements of a table$("table > tr:even") // numbering starts at 0Select the third descendant element of an element$("#faqs p:eq(2)") // numbering starts at 0Select all input elements with text as the type attribute$(":text")next([selector])prev([selector])attr(attributeName)attr(attributeName, value)css(propertyName)css(propertyName, value)addClass(className)removeClass([className])toggleClass(className)hide([duration])show([duration])each(function)Get the value of the src attribute of an image$("#image").attr("src");Set the value of the src attribute of an image to the value of a variable$("#image").attr("src", imageSource);Set the value of the color property of the h2 elements$("h2").css("color", "blue");Add a class to the h2 descendants of the faqs element$("#faqs h2").addClass("minus");Run a function for each element within an image_list element$("#image_list a").each(function() { // the statements of the function});ready(handler)unload(handler)error(handler)click(handler)toggle(handlerEven, handlerOdd)dblclick(handler)mouseenter(handler)mouseover(handler)mouseout(handler)hover(handlerIn, handlerOut)A handler for the double-click event of all text boxes that clears the clicked box$(":text").dblclick(function () { $(this).val("");}A handler for the hover event of each img element within a list$("#image_list img").hover( function() { alert("The mouse pointer has moved into an img element"); }, function() { alert("The mouse pointer has moved out of an img element); }); // end hoverEvent methodDescriptionbind(event, handler)Attach an event handler to an event.unbind(event, handler)Remove an event handler from an event.one(event, handler)Attach an event handler and remove it after it runs one time.trigger(event)Trigger the event for the selected element.var clearClick = function () { // the statements for the event handler }With the bind method$("#clear").bind(click, clearClick);With the shortcut method$("#clear").click(clearClick);How to attach an event handler to two different events$("#clear").click(clearClick);$(":text").dblclick(clearClick);$("#clear").unbind("click", clearClick);How to attach and remove an event handler so it runs only once$("#clear").one("click", confirmClick);With the trigger method$("#clear").trigger("click");With the shortcut method$("#clear").click();How to use the shortcut method to trigger an event from an event handler$(":text").dblclick(function () { $("#clear").click(); // triggers the click event // of the clear button}

jQuery FAQs What is jQuery? jQuery is a library of the JavaScript functions that you're most likely to need as you develop web sites. Why is jQuery becoming so popular? Three reasons:

  • It's free.
  • It lets you get more done in less time.
  • All of its functions are cross-browser compatible.

Which is harder to learn: jQuery or JavaScript? For most functions, jQuery is significantly easier to learn and use than JavaScript. But remember: jQuery is JavaScript.

$(document).ready(function() { $("#faqs h2").toggle( function() { $(this).addClass("minus"); $(this).next().show(); }, function() { $(this).removeClass("minus"); $(this).next().hide(); } ); // end toggle}); // end ready

Ram Tap Combined Test

James Allison 1-1

li { padding-right: 10px; display: inline;}$(document).ready(function() { // preload images $("#image_list a").each(function() { var swappedImage = new Image(); swappedImage.src = $(this).attr("href"); });

// set up event handlers for links $("#image_list a").click(function(evt) { // swap image var imageURL = $(this).attr("href"); $("#image").attr("src", imageURL); //swap caption var caption = $(this).attr("title"); $("#caption").text(caption); // cancel the default action of the link evt.preventDefault(); // jQuery cross-browser method }); // end click

// move focus to first thumbnail $("li:first-child a:first-child").focus();}); // end ready

Ram Tap Combined Test

$(document).ready(function() { $("#image_rollovers img").each(function() { var oldURL = $(this).attr("src"); var newURL = $(this).attr("id"); // preload images var rolloverImage = new Image(); rolloverImage.src = newURL;

// set up event handlers $(this).hover( function() { $(this).attr("src", newURL); }, function() { $(this).attr("src", oldURL); } ); // end hover }); // end each}); // end readyA challenging application thats similar to the FAQs applicationShow more or less of the text for a heading.Display the image associated with a link.Replace the JavaScript with jQuery.Display the answer to a question when the mouse is hovered over the question.12/06/12 9:52 AMChapter 07 Word slides.docxSlide 1