JQuery Getting Started
JQuery Getting Started
jQuery:
Installation, Overview,
and Getting Started
Slides © 2016 Marty Hall, [email protected]
For additional materials, please see http://www.coreservlets.com/. The JavaScript tutorial section contains
complete source code for all examples in the entire tutorial series, plus exercises and exercise solutions for each topic.
Overview of jQuery
For additional materials, please see http://www.coreservlets.com/. The JavaScript tutorial section contains
complete source code for all examples in the entire tutorial series, plus exercises and exercise solutions for each topic.
jQuery is the Single-Most Popular JavaScript Library
• Ajax utilities
– General: $.ajax(…), $(…).load(…)
– Shortcuts: $.get, $.post, $.getJSON
• DOM search and manipulation utilities
– $("p.myStyle").addClass("extraStyle").show();
• Simple animation
– Not as extensive as Scriptaculous, but easy to use
• Cross-browser event model
– Assigns handlers programmatically, hides browser differences
• General JavaScript utilities
– Functions operating on strings and arrays
• Rich GUIs
– jQuery UI provides widgets, fancier effects, drag/drop
6
7
Industry Usage (Google Searches)
Browser Compatibility
• Chrome, Firefox, Opera
– Current and previous major version
• Older versions tend to work, but are not tested on new jQuery code
• Same strategy as Google Docs
• Internet Explorer
– jQuery 1.9: IE 6 and later
– jQuery 2.x: IE 9 and later (same API as jQuery 1.9)
• As of 10/2015, no mention of Microsoft Edge on official support page, but work is in progress
• Safari
– 5.1 and later
• Android browser
– 4.0 and later
• iOS browser
– 6.1 and later
9
JavaScript Testing
• Problem (from first section on general JavaScript)
– Java: very strict compliance tests to be called “Java”
• You can have very high confidence that code written in Java 8 on Windows version will
run identically (except for some differences in how GUIs look) on Java 8 on MacOS,
Linux, Solaris, and other Windows versions. True for Java from Oracle, Apple, IBM, or
open source version from Brazil.
– JavaScript: every browser vendor does it themselves, with no outside checks
• Behavior of same JavaScript program can vary substantially from browser to browser,
and even from one release of the same browser to another
• Consequence
– Before final deployment, you must test on all browsers you expect to support
– One of main benefits of jQuery is that it tries to hides browser differences, and it
mostly succeeds
• But even so, you must test on full range of browsers before final deployment
10
Downloading and
Using jQuery
Slides © 2016 Marty Hall, [email protected]
For additional materials, please see http://www.coreservlets.com/. The JavaScript tutorial section contains
complete source code for all examples in the entire tutorial series, plus exercises and exercise solutions for each topic.
Downloading
• Main download site
– http://jquery.com/download/
• Development vs. deployment versions
– For development, use uncompressed file
• E.g., jquery-2.1.4.js
– For deployment, use compressed file
• E.g., jquery-2.1.4.min.js
• Rename file to generic name
– Rename file to jquery.js (or possibly jquery-2.js)
• Lets you switch from jquery-2.1.4.js to jquery-2.1.4.min.js without editing many HTML
files
• Similarly, lets you later upgrade to 2.1.5 without editing many HTML files
12
• You should load jQuery before loading your own scripts that make use of jQuery.
• You should rename jquery-x.y.z.js to jquery.js.
13
coreservlets.com – custom onsite training
Interactive Testing
and Practice
Slides © 2016 Marty Hall, [email protected]
For additional materials, please see http://www.coreservlets.com/. The JavaScript tutorial section contains
complete source code for all examples in the entire tutorial series, plus exercises and exercise solutions for each topic.
15
A Sample File: Part 1
<!DOCTYPE html>
<html>
<head>
<title>jQuery Test</title>
<link rel="stylesheet" href="css/styles.css"/>
<script src="scripts/jquery.js"></script>
</head>
16
Manipulating the
DOM with jQuery:
Basics Note: brief intro only. More details in later tutorial section.
For additional materials, please see http://www.coreservlets.com/. The JavaScript tutorial section contains
complete source code for all examples in the entire tutorial series, plus exercises and exercise solutions for each topic.
Idea
• Manipulating the DOM
– One of the main uses of jQuery is to find elements in the DOM (Document Object
Model – the tree structure that represents the HTML page) and modify them in
various ways
• Quick intro now
– You can’t do much with jQuery without simple DOM manipulation, so very simple
intro now
– This will be enough to do Ajax – make network requests to the server and get data
back
• Lots more detail later
– In section entirely devoted to DOM manipulation
21
Manipulating DOM: Overview
• Find HTML elements that match a pattern
– $("css selector pattern")
• Returns array of HTML elements that match
• Perform operations on the elements
– $("css selector pattern").op1(…);
• Single operation
– $("css selector pattern").op1(…).op2(…).op3(…);
• Consecutive operations via “chaining”
• Example
– $("div h3").addClass("yellow").hide("slow");
• Finds all h3’s that are inside a div, adds the CSS class named “yellow”, then slowly
makes them disappear
22
23
Manipulating DOM Elements: Commonly Used Functions
• $("#some-id").val()
– Returns value of input element. Used on 1-element sets.
• $("selector").each(function)
– Calls function on each element. “this” set to element.
• $("selector").addClass("name")
– Adds CSS class name to each. Also removeClass, toggleClass
• $("selector").hide()
– Makes invisible (display: none). Also show, fadeOut, fadeIn, etc.
• $("selector").click(function)
– Adds onclick handler. Also change, focus, mouseover, etc.
• $("selector").html("<tag>some html</tag>")
– Sets the innerHTML of each element. Also append, prepend
24
jQuery Selectors:
Quick Example
Slides © 2016 Marty Hall, [email protected]
For additional materials, please see http://www.coreservlets.com/. The JavaScript tutorial section contains
complete source code for all examples in the entire tutorial series, plus exercises and exercise solutions for each topic.
Idea
• Press button 1
– Change colors of certain elements
– Make them disappear
• Press button 2
– Make previously hidden elements reappear
• Purpose
– Give quick intro to use of jQuery for manipulating the DOM
– Little explanation now
– Lots more explanation in later sections
26
function setRandomStyle() {
$(this).addClass(randomStyle());
} Add “red”, “yellow” or “green” CSS names to each
matching element
27
Example: Randomizing Background Colors (Continued)
function randomStyle() {
var styles = ["red", "yellow", "green"];
return(randomElement(styles));
}
function randomElement(array) {
var index = Math.floor(Math.random()*array.length);
return(array[index]);
}
28
$(function() {
$("#button1").click(randomizeHeadings);
$("#button2").click(revertHeadings);
});
Sets onclick handlers
29
Example: Randomizing Colors (Style Sheet)
.red { background-color: red }
.yellow { background-color: yellow }
.green { background-color: green }
30
My file, containing code shown on previous slides. Should be loaded after the main jquery file.
31
Example: Randomizing Colors (HTML Continued)
…
<h3>Foo, bar, baz</h3>
<h3>Blah, blah, blah</h3>
<h3>Yadda, yadda, yadda</h3>
<h3>Foo, bar, baz</h3>
<h3>Blah, blah, blah</h3>
<h3>Yadda, yadda, yadda</h3>
<h3>Foo, bar, baz</h3>
<h3>Blah, blah, blah</h3> The ids to which click
handlers are attached.
Using “jQuery”
instead of “$”
Slides © 2016 Marty Hall, [email protected]
For additional materials, please see http://www.coreservlets.com/. The JavaScript tutorial section contains
complete source code for all examples in the entire tutorial series, plus exercises and exercise solutions for each topic.
Equivalent Names
• $
– Traditional name of main jQuery object, and the one we use throughout this tutorial
• $("h3").hide("slow");
• $.ajax({ url: "relative-address", success: handlerFunc });
• jQuery
– Alternative and completely equivalent name. Introduced because some other
JavaScript libraries (e.g., Prototype) also use the $ variable
• jQuery("h3").hide("slow");
• jQuery.ajax({ url: "relative-address", success: handlerFunc });
• If you use another library that uses $
– Load jQuery second
– Call jQuery.noConflict();
– Use jQuery.blah instead of $.blah
36
Wrap-Up
For additional materials, please see http://www.coreservlets.com/. The JavaScript tutorial section contains
complete source code for all examples in the entire tutorial series, plus exercises and exercise solutions for each topic.
Summary
• Rename jquery file and load it
– Rename jquery-2.1.x.js or jquery-2.1.x-min.js to jquery.js
• Load HTML file
– Either in Firefox (with Firebug installed) or Chrome
– HTML file should have some HTML elements and should load jquery.js
• Bring up Firebug
– Or Chrome developer tools
• Practice interactively
– Search for CSS selector patterns
$("h1")
$("#main-div ul ul li")
– Hide or show matching elements
$("#main-div ul ul li").hide("slow")
38
39
coreservlets.com – custom onsite training
For additional materials, please see http://www.coreservlets.com/. The JavaScript tutorial section contains
complete source code for all examples in the entire tutorial series, plus exercises and exercise solutions for each topic.