Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1....

181
Yearning jQuery

Transcript of Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1....

Page 1: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

Yearning jQuery

Page 2: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

Hi, I’m Remy / @rem

Screencast @ jqueryfordesigners.com

Questions: interrupt me & ask!

Page 3: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.
Page 4: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

1. Build without jQuery.

2. Design the start and end of your effects without jQuery.

3. Add jQuery a little at a time.

Page 5: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

"all about CSS"(a lot of it is...)

- Me, to many a colleague

Page 6: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

Dear designer/dev: if you're using JavaScript to do a job that CSS could have done perfectly well, you've lost some points in my book. Sorry

@remRemy Sharp

Page 7: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

The day someone loses business because an animated transition wasn't used to reveal a screenshot: I'll sell my left nut on eBay. #cssinstead

@remRemy Sharp

Page 8: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

What we're covering

• Getting friendly with the $

• DOM navigation & manipulation

• Events

• Ajax

• Tips

Page 9: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

APIsdocs.jquery.comapi.jquery.com

visualjquery.com

Blogs, tutorials, screencasts,plugins, development sprints

forum.jquery.com

IRC channelirc.freenode.net/#jquery

Twitter@jquery

@jquerysites@jqueryui

Help!

Page 10: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

jsbin.com

Page 11: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

Bling Function

Page 12: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.
Page 13: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

It means no more of this

var tables = document.getElementsByTagName('table');

for (var t = 0; t < tables.length; t++) {

! var rows = tables[t].getElementsByTagName('tr');

! for (var i = 1; i < rows.length; i += 2) {

if (!/(^|s)odd(s|$)/.test(rows[i].className)) {

rows[i].className += ' odd';

}

}

}

Page 14: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

jQuery simplifies

$('table tr:nth-child(odd)').addClass('odd');

Page 15: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

jQuery simplifies

$('table tr:nth-child(odd)').addClass('odd');

jQuery function

Page 16: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

jQuery simplifies

$('table tr:nth-child(odd)').addClass('odd');

jQuery function

CSS expression

Page 17: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

jQuery simplifies

$('table tr:nth-child(odd)').addClass('odd');

jQuery function

CSS expression

jQuery method

Page 18: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

jQuery simplifies

$('table tr:nth-child(odd)').addClass('odd');

Page 19: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

$('just css')

Page 20: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

Tools of the Trade

Page 21: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

• Firefox: Firebug (& FireQuery*)

• Safari & Chrome: Web Inspector

• Opera: DragonFly

• IE: Web Developer Toolbar

Page 22: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.
Page 23: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.
Page 24: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.
Page 25: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

$.fn.jquery

Tip

(little 'q')

Page 26: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

Let's play.

http://twitter.com

Page 27: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

jQuery on every site?

No Problem.

Page 28: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

http://bit.ly/9JAcCj

Page 29: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

Let's play.

Page 30: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

Installing jQuery

Page 31: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

dev or min?

Page 32: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

Hosting options• Host it yourself (good for offline

dev)

• Hotlink it:

• Media Temple

• Microsoft

• Google (my pick)http://docs.jquery.com/Downloading_jQuery

Page 33: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

CDN FTW

Page 34: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

Tip: keep a copy on your machine

Page 35: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

Where does it all go?

Page 36: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

• jQuery first

• Then jQuery plugins

• Then your code

Page 37: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

• Other JavaScript libraries

• jQuery last library

• Then jQuery plugin scripts

• Then your code

Page 38: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

<html><head> <styles> <!-- make me beautiful --> </styles></head><body> <content> <!-- make me learned --> </content> <behaviour> <!-- ooo, matron --> </behaviour></body></html>

Page 39: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

<html><head> <styles> <!-- make me beautiful --> </styles></head><body> <content> <!-- make me learned --> </content> <behaviour> <!-- ooo, matron --> </behaviour></body></html>

Styles first let's the page

render without scripts

blocking

Page 40: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

<html><head> <styles> <!-- make me beautiful --> </styles></head><body> <content> <!-- make me learned --> </content> <behaviour> <!-- ooo, matron --> </behaviour></body></html>

Then your content,

again so that it's delivered

to your visitor as early as possible

Page 41: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

<html><head> <styles> <!-- make me beautiful --> </styles></head><body> <content> <!-- make me learned --> </content> <behaviour> <!-- ooo, matron --> </behaviour></body></html>

Finally, add your

behaviour, the jQuery

and sexy magic jazz.

Page 42: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

$(document).ready(function () {

// < YOU >

});

Page 43: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

$(document).ready(function () {

// < YOU >

});

Page 44: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

$(function () {

// < YOU >

});

Page 45: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

jQuery(function ($) {

// < YOU >

});

Tip

Useful when giving code to a client

Page 46: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

<!DOCTYPE html><html lang="en"><head><meta charset=utf-8 /><title>My first jQuery page</title></head><body><h1>Remy woz 'ere</h1><p>Lorem ipsum dolor sit amet.</p><script src="jquery.min.js"></script><script>$(function () { // < YOU >});</script></body></html>

Page 47: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

Is it jQuery or $?

Page 48: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

It's both.

Page 49: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

$('table tr:nth-child(odd)')

Page 50: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

jQuery('table tr:nth-child(odd)')

Page 51: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

$('table tr:nth-child(odd)')

Page 52: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

∞∞∞ Chaining ∞∞∞

Page 53: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

$('div').show().hide().slideUp().slideDown();

Page 54: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

$('div').show().hide().slideUp().slideDown();

Get the divs,

Page 55: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

$('div').show().hide().slideUp().slideDown();

and show them,

Page 56: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

$('div').show().hide().slideUp().slideDown();

now hide them,

Page 57: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

$('div').show().hide().slideUp().slideDown();

and then slide them up,

Page 58: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

$('div').show().hide().slideUp().slideDown();

then finally slide them down.

Page 59: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

Can't chain text getters

.val() .html() .text() .css(prop) .attr(prop), etc

Page 60: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

if ( $('foo') ) { doAmazing();}

Gotcha & Tip

Page 61: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

1) $('foo')

2) $()

3) []

4) [].join(',') // works

Page 62: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

if ( $('foo') ) { doAmazing();}

Page 63: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

if ( $('foo').length ) { doAmazing();}

Page 64: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

$('el').doStuffvs

$.doStuff

Page 65: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

$('el').doStuffruns against everything

matched.

Page 66: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

$.doStuffis a utility.

Page 67: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

$.browser.version$.browser.msie$.browser.safari $.browser.webkit

Deprecated

Page 68: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

Doing stuff

Page 69: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

$('a').click(function () { alert('I got clicked'); return false;});

Page 70: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

$('a').click(function () { alert('I got clicked'); return false;});

Find the anchors

Page 71: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

$('a').click(function () { alert('I got clicked'); return false;});

when they're clicked

Page 72: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

$('a').click(function () { alert('I got clicked'); return false;});

run this function - don't worryabout the crazy syntax.

Page 73: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

$('a').click(function () { alert('I got clicked'); return false;});

Cancel the browser's default action, which is to redirect.

Page 74: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

$('a').click(function(event){ event.preventDefault(); alert('I got clicked');});

Same thing as cancelling (almost), this is useful for debugging.

Page 75: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

• click, dblclick

• mousedown, mouseup, mousemove mouseover, mouseout, mouseenter mouseleave

• keydown, keypress, keyup

• submit, change, blur, focus, select

• scroll, resize, load, error

Page 76: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

Tabs freakin' everywhere!

Page 77: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.
Page 78: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.
Page 79: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.
Page 80: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.
Page 81: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

You get the idea.

Page 82: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

• A group of "panels"

• Tabs (or links) pointing to a panel

• Clicking tab, hides all panels, then shows just one

A tabbing system is...

Page 83: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

...the content can also be be Ajaxy

Page 84: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

A few words on this

Page 85: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

this is the element

that the event is

happening to.

Page 86: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

$('a').click(function () { console.log(this);});

this is the anchor element, not the jQuery wrapped element.

Page 87: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

$('a').click(function () { setTimeout(function () { $(this).text("I'm alive!"); }, 1000);});

Common mistake

Page 88: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

$('a').click(function () { setTimeout(function () { $(this).text("I'm alive!"); }, 1000);});

Common mistake

this is the window object, not the link

Page 89: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

$('a').click(function () { var el = this; setTimeout(function () { $(el).text("I'm alive!"); }, 1000);});

Common mistake

Page 90: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

Live codin' time!

tabs.html

Page 91: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

Events

Page 92: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

$('input').keydown(function (event) { // what's the event arg?});

Page 93: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

•.keyup(fn), .click(fn), etc

•.bind(type, fn)

•.trigger(type), unbind(type)

•.one(type, fn)

Page 94: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

Clickables

Tip

Page 95: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

$.event.special.click = { setup: function() { $(this).css('cursor','pointer'); return false; }, teardown: function() { $(this).css('cursor',''); return false; }};

All credit to David Walsh

Page 96: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

Problem

Page 97: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

<h1>Super Ted</h1><img src="superted.jpg"><img src="spotty.jpg"><img src="txspete.jpg"><script src="jquery.js"><script>$('img').click(function(){ showDetails(this);});</script>

When the page has finished

loading, the jQuery runs

Page 98: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

<h1>Super Ted</h1><img src="superted.jpg"><img src="spotty.jpg"><img src="txspete.jpg"><script src="jquery.js"><script>$('img').click(function(){ showDetails(this);});</script>

Clicking these images

"shows details"

Page 99: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

<h1>Super Ted</h1><img src="superted.jpg"><img src="spotty.jpg"><img src="txspete.jpg"><script src="jquery.js"><script>$('img').click(function(){ showDetails(this);});</script>

Now Ajax (or something else) add new images to the

page

Page 100: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

<h1>Super Ted</h1><img src="superted.jpg"><img src="spotty.jpg"><img src="txspete.jpg"><img src="mothernature.jpg"><script src="jquery.js"><script>$('img').click(function(){ showDetails(this);});</script>

Now Ajax (or something else) add new images to the

page

Page 101: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

<h1>Super Ted</h1><img src="superted.jpg"><img src="spotty.jpg"><img src="txspete.jpg"><img src="mothernature.jpg"><script src="jquery.js"><script>$('img').click(function(){ showDetails(this);});</script>

Clicking this image doesn't do anything.

Page 102: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

Solution: delegation

Page 103: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

<h1>Super Ted</h1><img src="superted.jpg"><img src="spotty.jpg"><img src="txspete.jpg"><script src="jquery.js"><script>$('#superTed').delegate('img','click',function(){ showDetails(this);});</script>

Page 104: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.
Page 105: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

These images are loaded after

the DOM has loaded via Ajax

Page 106: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

We're going to "delegate" the task of listening for particular

events to this <div>

Page 107: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

We've "delegate" clicks, looking for the "img" selector

Page 108: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

$('div').delegate('img','click',function(){ /* do funky stuff */ });

Page 109: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

Now, any new images inserted in this <div> can be clicked and will fire the

"funky stuff" function

Page 110: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

Ajax Warning: wear your tech-hat

Page 111: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

No brainer Ajax

Page 112: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

.load

Page 113: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.
Page 114: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

Example

Page 115: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

$('#detail').load('page.html');

Page 116: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

$('#detail').load('page.html #id');

Page 117: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

1. Ajax load the page

2. Search for the selector passed (#dizzy)

3. Squirt contents found into original selector

Page 118: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

$('#tabs a').click(function (event) { // this.pathname = 'ajax-load-detail.html' $('#detail').load(this.pathname); return false;});

Page 119: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

$('#tabs a').click(function (event) { $('#detail').load(this.pathname + ' ' + this.hash); return false;});

this.hash

Page 120: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

JSON

Page 121: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

{ screen_name : "@rem", height : "short", fingers : 5, brit : true}

JavaScript Object

Page 122: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

{ "screen_name": "@rem", "height": "short", "fingers": 5, "brit": true}

JSON

Page 123: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

JSONP WTF

Page 124: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

{ "screen_name": "@rem", "height": "short", "fingers": 5, "brit": true}

JSON+...

Page 125: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

callback({ "screen_name": "@rem", "height": "short", "fingers": 5, "brit": true});

JSON+Padding

Page 126: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

Getting other people's data

Page 127: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

$.getJSON

Page 128: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

Remember =?

Page 129: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

Twitter

ajax/twitter.html

Page 130: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

$(document).ready(function () { $.getJSON('http://twitter.com/statuses/user_timeline/rem.json?callback=?', function (data) { $('#tweets').empty();

$.each(data, function (i, item) { $('#tweets').append('<li class="tweet">' + item.text + '</li>'); }); });});

Page 131: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

Giving users feedback

Loading...

Page 132: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

Ajax order

1. ajaxStart

2. ajaxSuccess (or ajaxError)

3. ajaxComplete

http://api.jquery.com/?s=ajax

Page 133: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

$('#status').ajaxStart(function () { $(this).fadeIn();}).ajaxComplete(function () { $(this).fadeOut();});

Page 134: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

Deferreds

Page 135: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

$.get('foo.html', function (html) { $('#latest').append(html);});

var jqXHR = $.get('foo.html');

jqXHR.done(function (html) { $('#latest').append(html);});

Page 136: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

$.ajax({ url: 'foo.json', dataType: 'json', success: function () { // this === xhr }, error: function () { }});

Page 137: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

$.ajax({ url: 'foo.json', dataType: 'json', context: document.body, success: function () { // this === body element }, error: function () { }});

Page 138: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

var jqXHR = $.ajax({ url: 'foo.json', dataType: 'json', context: document.body});

jqXHR.then(success, fail);

Page 139: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

var jqXHR = $.ajax({ url: 'foo.json', dataType: 'json', context: document.body});

jqXHR.then(success, fail);

// much later in the code

jqXHR.done(success2);

Page 140: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

jqXHR is a promise

Page 141: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.
Page 142: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.
Page 143: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

var jqXHR = $.ajax({ url: 'foo.json', dataType: 'json', context: document.body});

// much later in the code

jqXHR.done(success);

Page 144: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

.done(ok) // success

.fail(fail) // error

.always(fn) // complete

.then(ok, fail)

Page 145: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

Plugins

Page 146: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

What’s a plugin?A plugin is nothing more than a custom jQuery method created to extend the functionality of the jQuery object

$(‘ul’).myPlugin()

Page 147: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

Plugin design in 6 steps

Page 148: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

Step 1:

create a private scope for $ alias

Page 149: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

<!DOCTYPE html><html><body><script src=”jquery.js”></script><script>(function ($) {

})(jQuery);</script></body></html>

Page 150: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

Step 2:

attach plugin to fn alias (aka prototype)

Page 151: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

<!DOCTYPE html><html><body><script src=”jquery.js”></script><script>(function ($) {$.fn.notHate = function () { $(this).text( $(this).text().replace(/hate/g, ‘love’) );}; // end of plugin})(jQuery);</script></body></html>

Page 152: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

<!DOCTYPE html><html><body><p>I hate jQuery!</p><script src=”jquery.js”></script><script>(function ($) {$.fn.notHate = function () { $(this).text( $(this).text().replace(/hate/g, ‘love’) );}; // end of plugin})(jQuery);

$(‘p’).notHate();</script></body></html>

Page 153: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

Step 3:

add implicit iteration

Page 154: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

<!DOCTYPE html><html><body><p>I hate jQuery!</p><p>I mean really hate jQuery!</p><script src=”jquery.js”></script><script>(function ($) {$.fn.notHate = function () { this.each(function () { $(this).text( $(this).text().replace(/hate/g, ‘love’) ); });}; // end of plugin})(jQuery);

$(‘p’).notHate();</script></body></html>

Page 155: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

Step 4:

enable chaining

Page 156: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

<!DOCTYPE html><html><body><p>I hate jQuery!</p><p>I mean really hate jQuery!</p><script src=”jquery.js”></script><script>(function ($) {$.fn.notHate = function () { return this.each(function () { $(this).text( $(this).text().replace(/hate/g, ‘love’) ); });}; // end of plugin})(jQuery);

$(‘p’).notHate().hide();</script></body></html>

Page 157: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

<!DOCTYPE html><html><body><p>I hate jQuery!</p><p>I mean really hate jQuery!</p><script src=”jquery.js”></script><script>(function ($) {$.fn.notHate = function () { return this.each(function () { $(this).text( $(this).text().replace(/hate/g, ‘love’) ); });}; // end of plugin})(jQuery);

$(‘p’).notHate().hide();</script></body></html>

this == jQuery

Page 158: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

<!DOCTYPE html><html><body><p>I hate jQuery!</p><p>I mean really hate jQuery!</p><script src=”jquery.js”></script><script>(function ($) {$.fn.notHate = function () { return this.each(function () { $(this).text( $(this).text().replace(/hate/g, ‘love’) ); });}; // end of plugin})(jQuery);

$(‘p’).notHate().hide();</script></body></html>

this == DOM element

this == jQuery

Page 159: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

Step 5:

add default options

Page 160: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

<!DOCTYPE html><html><body><p>I hate jQuery!</p><p>I mean really hate jQuery!</p><script src=”jquery.js”></script><script>(function ($) {$.fn.notHate = function () { return this.each(function () { $(this).text( $(this).text().replace(/hate/g, ➥ $.fn.notHate.defaults.text) ); });}; // end of plugin$.fn.notHate.defaults = {text:‘love’};})(jQuery);

$(‘p’).notHate();</script></body></html>

Page 161: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

Step 6:

add custom options

Page 162: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

<!DOCTYPE html><html><body><p>I hate jQuery!</p><p>I mean really hate jQuery!</p><script src=”jquery.js”></script><script>(function ($) {$.fn.notHate = function () { return this.each(function () { $(this).text( $(this).text().replace(/hate/g, ➥ $.fn.notHate.defaults.text) ); });}; // end of plugin$.fn.notHate.defaults = {text:‘love’};})(jQuery);

$(‘p’).notHate({text: ‘love-love-love’});</script></body></html>

Page 163: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

<!DOCTYPE html><html><body><p>I hate jQuery!</p><p>I mean really hate jQuery!</p><script src=”jquery.js”></script><script>(function ($) {$.fn.notHate = function (options) { return this.each(function () { $(this).text( $(this).text().replace(/hate/g, ➥ $.fn.notHate.defaults.text) ); });}; // end of plugin$.fn.notHate.defaults = {text:‘love’};})(jQuery);

$(‘p’).notHate({text: ‘love-love-love’});</script></body></html>

Page 164: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

<!DOCTYPE html><html><body><p>I hate jQuery!</p><p>I mean really hate jQuery!</p><script src=”jquery.js”></script><script>(function ($) {$.fn.notHate = function (options) { var settings = $.extend({}, ➥ $.fn.notHate.defaults, options);

return this.each(function () { $(this).text( $(this).text().replace(/hate/g, ➥ $.fn.notHate.defaults.text) ); });}; // end of plugin$.fn.notHate.defaults = {text:‘love’};})(jQuery);

$(‘p’).notHate({text: ‘love-love-love’});</script></body></html>

Page 165: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

<!DOCTYPE html><html><body><p>I hate jQuery!</p><p>I mean really hate jQuery!</p><script src=”jquery.js”></script><script>(function ($) {$.fn.notHate = function (options) { var settings = $.extend({}, ➥ $.fn.notHate.defaults, options);

return this.each(function () { $(this).text( $(this).text().replace(/hate/g, ➥ settings.text) ); });}; // end of plugin$.fn.notHate.defaults = {text:‘love’};})(jQuery);

$(‘p’).notHate({text: ‘love-love-love’});</script></body></html>

http://jsbin.com/ifuga/edit

Page 166: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.
Page 167: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

<!DOCTYPE html><html><body><p>I hate jQuery!</p><p>I mean really hate jQuery!</p><script src=”jquery.js”></script><script>(function ($) {$.fn.notHate = function (options) { var settings = $.extend({}, ➥ $.fn.notHate.defaults, options);

return this.each(function () { $(this).text( $(this).text().replace(/hate/g, ➥ settings.text) ); });}; // end of plugin$.fn.notHate.defaults = {text:‘love’};})(jQuery);

$(‘p’).notHate({text: ‘love-love-love’});</script></body></html>

Page 168: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

<!DOCTYPE html><html><body><p>I hate jQuery!</p><p>I mean really hate jQuery!</p><script src=”jquery.js”></script><script>(function ($) {$.fn.notHate = function (options) { var settings = $.extend({}, ➥ $.fn.notHate.defaults, options);

return this.text(function (i, text) { return text.replace(/hate/g, ➥ settings.text); });}; // end of plugin$.fn.notHate.defaults = {text:‘love’};})(jQuery);

$(‘p’).notHate({text: ‘love-love-love’});</script></body></html>

Page 169: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

Poorly designed plugins

Page 170: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

$.fn.myplugin = function () {  var me = $(this).each(function() {    return $(this).bind('someEvent', function () {      // does something    });  });

  return me;};

Page 171: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

$.fn.myplugin = function () {  var me = $(this).each(fn);

  return me;};

Page 172: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

$.fn.myplugin = function () {  return $(this).each(fn);};

Page 173: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

$.fn.myplugin = function () {  return $(this).each(function() {    return $(this).bind('someEvent', function () {      // does something    });  });};

Page 174: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

$.fn.myplugin = function () {  return $(this).each(function() {    return $(this).bind('someEvent', function () {      // does something    });  });};

Page 175: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

$.fn.myplugin = function () {  return this.each(function() {    return $(this).bind('someEvent', function () {      // does something    });  });};

Page 176: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

$.fn.myplugin = function () {  return this.each(function() {    return $(this).bind('someEvent', function () {      // does something    });  });};

Page 177: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

$.fn.myplugin = function () {  return this.each(function() {    $(this).bind('someEvent', function () {      // does something    });  });};

Page 178: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

$.fn.myplugin = function () {  return this.each(function() {    $(this).bind('someEvent', function () {      // does something    });  });};

Page 179: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

$.fn.myplugin = function () { return this.bind('someEvent', function () {  // does something  });};

Page 180: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

(function ($) { $.fn.myplugin = function () {   return this.bind('someEvent', function () {    // does something  }); };})(jQuery);

Page 181: Yearning jQuery - O'Reilly Mediaassets.en.oreilly.com/1/event/61/Learning jQuery Paper.pdf · 1. Build without jQuery. 2. Design the start and end of your effects without jQuery.

Questions?To contact me after my presentation – text NHT to INTRO (46876)

Or [email protected]@rem