Beginning jQuery

32

Click here to load reader

description

Updated with new exercises - March 2014. Introduction to jQuery (for journalism students) and review of the Code School "Try jQuery" course, Parts 1-3.

Transcript of Beginning jQuery

Page 1: Beginning jQuery

Beginning jQuery

Review of the Code School course“Try jQuery” (parts 1–3)

Page 2: Beginning jQuery
Page 3: Beginning jQuery

What is jQuery?

• jQuery is JavaScript library• A “library” in code is the same as the

“modules” you used in Python– You import the module, which contains a lot of

functions– Then you can “call” (use) those functions in your

code• The entire library is written in JavaScript

Page 4: Beginning jQuery

Lots of resources — http://learn.jquery.com/

Page 5: Beginning jQuery

How can you include jQuery in your own Web pages?

Page 6: Beginning jQuery

Step 1: Include a link to jquery.min.js in the HEAD of each HTML file.

(Note the Google URL.)

Page 7: Beginning jQuery

Frequently asked questions

1. Why use the jQuery file hosted at Google?You don’t need to download jquery.js, and since many websites do it this way, the file might already be cached by the user’s browser.

2. Why put the SCRIPT tag in the HEAD and not the BODY?

There is always a debate about this, including an assertion that the page loads faster if the SCRIPT tag is at the bottom, right before the closing </body> tag. Others say all SCRIPT tags should be in the HEAD.

3. Why start the URL with http:// instead of // alone?You’ll see many pages that leave off the http: in the SCRIPT tag for Google’s jquery.js. This works on a Web server but not locally, when the files are on your hard drive.

Page 8: Beginning jQuery

Step 2: Include a link to your own .js file in the HEAD of the HTML file.

Place it after the Google jQuery link.

Page 9: Beginning jQuery

Step 3: Write your own .js file correctly (so it works!).

Page 10: Beginning jQuery

A well-organized website uses folders.

Page 11: Beginning jQuery

What you need to get started

1. An HTML document – With a link to Google’s latest jquery.min.js file– With a link to your own .js file

2. A JavaScript document or file of your own– With all the jQuery code between:

$(document).ready(function(){// and

});

Page 12: Beginning jQuery

Review of Levels 1–3http://try.jquery.com/

Page 13: Beginning jQuery

Download the exercise files!https://github.com/macloo/

jquery_beginners

[link]

Page 14: Beginning jQuery

Calling jQuery with $

• Any code statement or instruction that uses jQuery uses the character “$”

• This will find the text enclosed in H1 tags:$("h1").text();

• This will change the text enclosed in H1 tags:$("h1").text("ABC123");

It will change all H1 tags on the page.

Exercises 1 & 2

Page 15: Beginning jQuery

Find, change an ID or a class

$("#wrapper"); // find the ID wrapper$(".item"); // find the class item

$("#wrapper").append("<p>Hooray!</p>");$(".item").append("<p>Hooray!</p>");

Quotation marks may be single or double.Exercise 3

Page 16: Beginning jQuery

Searching the DOM

$("#destinations li");Selects only the LI tags inside the element with the ID “destinations.” (In the example, the element is a UL.) This would also include all LI tags inside any nested list that was inside the “destinations” list.

$("#destinations > li");Selects only “direct descendants.” The greater-than sign limits this to only the LIs that are direct children of the ID “destinations.” No LIs in any nested lists will be affected.

Page 17: Beginning jQuery

Searching the DOM (2)

$("#destinations li:first");$("#destinations li:last");$("#destinations li:odd");$("#destinations li:even");

These are called “pseudo classes.” They select just what they say. Can be used to style tables dynamically, for example.

Exercise 4

Page 18: Beginning jQuery

“Traversing” the DOM

$("#tours").find("li").last();$("#tours").find("li").first().next();

$("#tours").find("li").first().parent();This one goes “up” the DOM to find the direct parent of the first LI.

$("#tours").children("li");This finds only the LIs that are the direct children of the list with the ID “tours.”

Page 19: Beginning jQuery

Summary of Levels 1 & 2

• We can use jQuery to target an element that has an ID or a class in the HTML: "#wrapper", ".photo", etc.

• We can use jQuery to target any element, using its tag or selector: "h1", "p", "div", etc.

• We can use jQuery to “walk” up and down the DOM: next(), first(), parent(), etc.

Page 20: Beginning jQuery

Level 3: Changing things

.append(x) // at end, inside

.prepend(x) // at top, inside

.before(x) // at top, outside

.after(x) // at end, outside

Example:$('.vacation').append(price);Inside the element with the class vacation, at the bottom, add the value of price. (Hint: price is a variable!)

Page 21: Beginning jQuery

Changing things (2)

.appendTo(x) // end, inside

.prependTo(x) // top, inside

.insertBefore(x) // top, outside

.insertAfter(x) // end, outside

Example: price.appendTo($('.vacation'));

Inside the element with the class vacation, at the bottom, add the value of price.

open file adding_things.html

Page 23: Beginning jQuery

Changing things (3)

$('button').remove();

We can use jQuery to completely remove an element from the HTML page.In the example at Code School, when we click the button, the button disappears and is replaced by a price.(Note: It will be gone forever.)

Page 24: Beginning jQuery

A function in jQuery

$('button').on('click', function() {// some code here

});

This function doesn’t have a name. It calls itself when someone clicks the button. Whatever code you write in the middle will run every time the button (any button) is clicked.

Page 25: Beginning jQuery

A function in jQuery (2)

$('.choice').on('click', function() {// some code here

});

This function is exactly the same as the previous one, but instead of using the name of the HTML tag (button), here we use a class assigned to that button (.choice).

Exercise 5

Page 26: Beginning jQuery

The keyword this

• When you write a function, often you want it to work for more than one thing.

• Example: You want to build a slideshow with a thumbnail image for each photo. Click a thumbnail, and the photo changes.

• How can you use the same function for every thumbnail and photo in the set?

Exercise 6:open file photos.html

Page 27: Beginning jQuery

$('.big').hide();var current = $('#pics').find('.big').first();$(current).show().addClass('view');$('#pics').before(current); // see below

$('.thumb').on('click', function() {$(current).remove();current = $(this).prev(); $('#pics').before(current); // see

below$(current).show().addClass('view');

});

// $(z).before(x) - x at top, outside zExercise 6

Page 28: Beginning jQuery

When we move an element in the DOM, it really moves.

It isn’t in the same place, even though your source HTML

does not change. (This is kind of weird.)

The large photo disappears from the DOMafter it is removed.

Page 29: Beginning jQuery

A little jQuery excitement!

• Tablesorter is a free jQuery plugin that helps you make beautiful tables:

http://tablesorter.com/docs/#Demo • That’s a fancy demo. Here’s a simpler demo:

http://macloo.github.io/jquery_beginners/script05.html

The demo comes from JavaScript: Visual QuickStart Guide (8th ed.), chapter 15 [ link ]

Page 30: Beginning jQuery
Page 31: Beginning jQuery
Page 32: Beginning jQuery

Beginning jQuery

Mindy McAdamsUniversity of Florida

March 2014