Download - jQuery Loves Developers - Oredev 2009

Transcript
Page 1: jQuery Loves Developers - Oredev 2009

jQuery <3 Developers

Page 2: jQuery Loves Developers - Oredev 2009

• Who, what, why of jQuery

• Core concepts of jQuery

• API overview & tips

• Plugin design pattern

• News

• Live demo

Overview

Page 3: jQuery Loves Developers - Oredev 2009

Quick survey first

Page 4: jQuery Loves Developers - Oredev 2009

Who?

Page 5: jQuery Loves Developers - Oredev 2009

Who?

Page 6: jQuery Loves Developers - Oredev 2009

Who?

reddit.comespn.comibm.com

stackoverflow.comboxee.tv

bit.lytwitpic.com

whitehouse.govmicrosoft.comamazon.comnetflix.combing.com

monster.comtv.com

overstock.comtime.com

capitalone.comwordpress.com

dell.comtwitter.com

w3.org

http://trends.builtwith.com/javascript/jquery

Page 7: jQuery Loves Developers - Oredev 2009

What?

• Selector engine: Sizzle

• DOM manipulation

• Events

• Effects

• Ajax

jQuery is a JavaScript library.

Page 8: jQuery Loves Developers - Oredev 2009

What does that mean?

Page 9: jQuery Loves Developers - Oredev 2009

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 10: jQuery Loves Developers - Oredev 2009

jQuery simpli!es

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

Page 11: jQuery Loves Developers - Oredev 2009

jQuery simpli!es

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

jQuery function

Page 12: jQuery Loves Developers - Oredev 2009

jQuery simpli!es

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

jQuery function

CSS expression

Page 13: jQuery Loves Developers - Oredev 2009

jQuery simpli!es

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

jQuery function

CSS expression

jQuery method

Page 14: jQuery Loves Developers - Oredev 2009

jQuery simpli!es

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

Page 15: jQuery Loves Developers - Oredev 2009

Why use jQuery

• Helps us to simplify and speed up web development

• Allows us to avoid common headaches associated with browser development

• Provides a large pool of plugins

• Large and active community

• Tested on 50 browsers, 11 platforms

• For both coder and designer (we don't discriminate)

Page 16: jQuery Loves Developers - Oredev 2009

Why use jQuery

• Helps us to simplify and speed up web development

• Allows us to avoid common headaches associated with browser development

• Provides a large pool of plugins

• Large and active community

• Tested on 50 browsers, 11 platforms

• For both coder and designer (we don't discriminate)

Page 17: jQuery Loves Developers - Oredev 2009

Help!

Page 18: jQuery Loves Developers - Oredev 2009

APIsdocs.jquery.comapi.jquery.com

visualjquery.com

Blogs, tutorials, screencasts,plugins, development sprints

Google Groupsjquery-enjquery-dev

jquery-ui-enjquery-ui-devjquery-a11y

IRC channelirc.freenode.net/#jquery

Twitter@jquery

@jquerysites@jqueryui

Help!

Page 19: jQuery Loves Developers - Oredev 2009

APIsdocs.jquery.comapi.jquery.com

visualjquery.com

Blogs, tutorials, screencasts,plugins, development sprints

Google Groupsjquery-enjquery-dev

jquery-ui-enjquery-ui-devjquery-a11y

IRC channelirc.freenode.net/#jquery

Twitter@jquery

@jquerysites@jqueryui

Help!

Page 20: jQuery Loves Developers - Oredev 2009

Concept 1:

knowing the jQuery parameter types

Page 21: jQuery Loves Developers - Oredev 2009

• CSS selectors & custom CSS expressions

• HTML

• DOM elements

• A function (shortcut for DOM ready)

Page 22: jQuery Loves Developers - Oredev 2009

• CSS selectors & custom CSS expressions

• HTML

• DOM elements

• A function (shortcut for DOM ready)

jQuery(‘div’) & jQuery(‘:first’)

Page 23: jQuery Loves Developers - Oredev 2009

• CSS selectors & custom CSS expressions

• HTML

• DOM elements

• A function (shortcut for DOM ready)

jQuery(‘div’) & jQuery(‘:first’)

jQuery(‘<li><a href=”#”>link</a></li>’)

Page 24: jQuery Loves Developers - Oredev 2009

• CSS selectors & custom CSS expressions

• HTML

• DOM elements

• A function (shortcut for DOM ready)

jQuery(‘div’) & jQuery(‘:first’)

jQuery(‘<li><a href=”#”>link</a></li>’)

jQuery(document) or jQuery(document.createElement(‘a’))

Page 25: jQuery Loves Developers - Oredev 2009

• CSS selectors & custom CSS expressions

• HTML

• DOM elements

• A function (shortcut for DOM ready)

jQuery(‘div’) & jQuery(‘:first’)

jQuery(‘<li><a href=”#”>link</a></li>’)

jQuery(document) or jQuery(document.createElement(‘a’))

jQuery(function(){}) = jQuery(document).ready(function(){})

Page 26: jQuery Loves Developers - Oredev 2009

• CSS selectors & custom CSS expressions

• HTML

• DOM elements

• A function (shortcut for DOM ready)

jQuery(‘div’) & jQuery(‘:first’)

jQuery(‘<li><a href=”#”>link</a></li>’)

jQuery(document) or jQuery(document.createElement(‘a’))

jQuery(function(){}) = jQuery(document).ready(function(){})

Page 27: jQuery Loves Developers - Oredev 2009

Concept 2:

!nd something,do something

Page 28: jQuery Loves Developers - Oredev 2009

<!DOCTYPE html><html><body> <ul id="nav"> <li><a>home</a></li> <li><a>about</a></li> </ul></body></html>

Page 29: jQuery Loves Developers - Oredev 2009

<!DOCTYPE html><html><body> <ul id="nav"> <li><a>home</a></li> <li><a>about</a></li> </ul><script src="jquery.js"></script></body></html>

Page 30: jQuery Loves Developers - Oredev 2009

<!DOCTYPE html><html><body> <ul id="nav"> <li><a>home</a></li> <li><a>about</a></li> </ul><script src="jquery.js"></script><script> jQuery('#nav li');</script></body></html>

Page 31: jQuery Loves Developers - Oredev 2009

<!DOCTYPE html><html><body> <ul id="nav"> <li class="item"><a>home</a></li> <li class="item"><a>about</a></li> </ul><script src="jquery.js"></script><script> jQuery('#nav li').addClass('item');</script></body></html>

Page 32: jQuery Loves Developers - Oredev 2009

Concept 3:

create something,do something

Page 33: jQuery Loves Developers - Oredev 2009

<!DOCTYPE html><html><body> <ul id='nav'> </ul></body></html>

Page 34: jQuery Loves Developers - Oredev 2009

<!DOCTYPE html><html><body> <ul id='nav'> </ul><script src=”jquery.js”></script><script> jQuery(‘<li>home</li>’); jQuery(‘<li>about</li>’);</script></body></html>

Page 35: jQuery Loves Developers - Oredev 2009

<!DOCTYPE html><html><body> <ul id='nav'> <li>home</li> <li>about</li> </ul><script src=”jquery.js”></script><script> jQuery(‘<li>home</li>’)➥.appendTo(‘#nav’); jQuery(‘<li>about</li>’)➥.appendTo(‘#nav’);</script></body></html>

Page 36: jQuery Loves Developers - Oredev 2009

createDocumentFragment

Insidetip

Page 37: jQuery Loves Developers - Oredev 2009

createDocumentFragment

Insidetip

(IE6 || IE7) && innerHTML && HTML5== fail

Page 38: jQuery Loves Developers - Oredev 2009

Concept 4:

chaining & operating

Page 39: jQuery Loves Developers - Oredev 2009

<!DOCTYPE html><html><body> <ul id='nav'> <li class=”item”><a href=”/home”>home</a></li> <li class=”item”><a href=”/about”>about</a></li> </ul><script src=”jquery.js”></script><script> jQuery(‘ul’).attr(‘id’, ‘nav’); jQuery(‘#nav li’).addClass(‘item’); jQuery(‘#nav a’).each(function () { jQuery(this).attr(‘href’, ‘/’+jQuery(this).text()); });</script></body></html>

Page 40: jQuery Loves Developers - Oredev 2009

<!DOCTYPE html><html><body> <ul id='nav'> <li class=”item”><a href=”/home”>home</a></li> <li class=”item”><a href=”/about”>about</a></li> </ul><script src=”jquery.js”></script><script> jQuery(‘ul’).attr(‘id’, ‘nav’); jQuery(‘#nav li’).addClass(‘item’); jQuery(‘#nav a’).each(function () { jQuery(this).attr(‘href’, ‘/’+jQuery(this).text()); });</script></body></html>

1

1

Page 41: jQuery Loves Developers - Oredev 2009

<!DOCTYPE html><html><body> <ul id='nav'> <li class=”item”><a href=”/home”>home</a></li> <li class=”item”><a href=”/about”>about</a></li> </ul><script src=”jquery.js”></script><script> jQuery(‘ul’).attr(‘id’, ‘nav’); jQuery(‘#nav li’).addClass(‘item’); jQuery(‘#nav a’).each(function () { jQuery(this).attr(‘href’, ‘/’+jQuery(this).text()); });</script></body></html>

1

1 2

2

Page 42: jQuery Loves Developers - Oredev 2009

<!DOCTYPE html><html><body> <ul id='nav'> <li class=”item”><a href=”/home”>home</a></li> <li class=”item”><a href=”/about”>about</a></li> </ul><script src=”jquery.js”></script><script> jQuery(‘ul’).attr(‘id’, ‘nav’); jQuery(‘#nav li’).addClass(‘item’); jQuery(‘#nav a’).each(function () { jQuery(this).attr(‘href’, ‘/’+jQuery(this).text()); });</script></body></html>

1

1 2

2

3

3

Page 43: jQuery Loves Developers - Oredev 2009

<!DOCTYPE html><html><body> <ul id='nav'> <li class=”item”><a href=”/home”>home</a></li> <li class=”item”><a href=”/about”>about</a></li> </ul><script src=”jquery.js”></script><script> jQuery(‘ul’).attr(‘id’, ‘nav’); jQuery(‘#nav li’).addClass(‘item’); jQuery(‘#nav a’).each(function () { jQuery(this).attr(‘href’, ‘/’+jQuery(this).text()); });</script></body></html>

Page 44: jQuery Loves Developers - Oredev 2009

<!DOCTYPE html><html><body> <ul id='nav'> <li class=”item”><a href=”/home”>home</a></li> <li class=”item”><a href=”/about”>about</a></li> </ul><script src=”jquery.js”></script><script> jQuery(‘ul’) .attr(‘id’, ‘nav’) .find(‘li’) .addClass(‘item’) .find(‘a’) .each(function () { jQuery(this).attr(‘href’, ‘/’+jQuery(this).text()); });</script></body></html>

Page 45: jQuery Loves Developers - Oredev 2009

jQuery returns itself containing the current

DOM collection

Page 46: jQuery Loves Developers - Oredev 2009

Concept 5:

understanding implicit iteration

Page 47: jQuery Loves Developers - Oredev 2009

<!DOCTYPE html><html><body> <ul id='nav'> <li class=”item”><a href=”/home”>home</a></li> <li class=”item”><a href=”/about”>about</a></li> </ul><script src=”jquery.js”></script><script> jQuery(‘ul’) .attr(‘id’, ‘nav’) .find(‘li’) .addClass(‘item’) .find(‘a’) .each(function () { jQuery(this).attr(‘href’, ‘/’+jQuery(this).text()); });</script></body></html>

Page 48: jQuery Loves Developers - Oredev 2009

<!DOCTYPE html><html><body> <ul id='nav'> <li class=”item”><a href=”/home”>home</a></li> <li class=”item”><a href=”/about”>about</a></li> </ul><script src=”jquery.js”></script><script> jQuery(‘ul’) .attr(‘id’, ‘nav’) .find(‘li’) .addClass(‘item’) .find(‘a’) .each(function () { jQuery(this).attr(‘href’, ‘/’+jQuery(this).text()); });</script></body></html>

Page 49: jQuery Loves Developers - Oredev 2009

• Knowing the jQuery parameter types

• Find something, do something

• Create something, do something

• Chaining & Operating

• Understanding Implicit Iteration

Review

Page 50: jQuery Loves Developers - Oredev 2009

“What about the bling function?”

Page 51: jQuery Loves Developers - Oredev 2009

jQuery == $

Page 52: jQuery Loves Developers - Oredev 2009

$ == jQuery

Page 53: jQuery Loves Developers - Oredev 2009

$ is an alias to jQuery

Page 54: jQuery Loves Developers - Oredev 2009

<!DOCTYPE html><html><body> <ul id='nav'> <li class=”item”>home</li> <li class=”item”>about</li> </ul><script src=”jquery.js”></script><script> jQuery(‘li’).addClass(‘item’);</script></body></html>

Page 55: jQuery Loves Developers - Oredev 2009

<!DOCTYPE html><html><body> <ul id='nav'> <li class=”item”>home</li> <li class=”item”>about</li> </ul><script src=”jquery.js”></script><script>

</script></body></html>

jQuery(‘li’).addClass(‘item’);

Page 56: jQuery Loves Developers - Oredev 2009

<!DOCTYPE html><html><body> <ul id='nav'> <li class=”item”>home</li> <li class=”item”>about</li> </ul><script src=”jquery.js”></script><script>

</script></body></html>

$(‘li’).addClass(‘item’);

Page 57: jQuery Loves Developers - Oredev 2009

<!DOCTYPE html><html><body> <ul id='nav'> <li class=”item”>home</li> <li class=”item”>about</li> </ul><script src=”jquery.js”></script><script> $(‘li’).addClass(‘item’);</script></body></html>

Page 58: jQuery Loves Developers - Oredev 2009

More than ready

Page 59: jQuery Loves Developers - Oredev 2009

<!DOCTYPE html><html><body> <ul id='nav'> <li class=”item”>home</li> <li class=”item”>about</li> </ul><script src=”jquery.js”></script><script> $(‘li’).addClass(‘item’);</script></body></html>

Page 60: jQuery Loves Developers - Oredev 2009

<!DOCTYPE html><html><body> <ul id='nav'> <li class=”item”>home</li> <li class=”item”>about</li> </ul><script src=”jquery.js”></script><script>$(document).ready(function () { $(‘li’).addClass(‘item’);});</script></body></html>

Page 61: jQuery Loves Developers - Oredev 2009

<!DOCTYPE html><html><body> <ul id='nav'> <li class=”item”>home</li> <li class=”item”>about</li> </ul><script src=”jquery.js”></script><script>$(document).ready(function () { $(‘li’).addClass(‘item’);});</script></body></html>

Page 62: jQuery Loves Developers - Oredev 2009

$(document).ready(function () { $(‘li’).addClass(‘item’);});

app.js

Page 63: jQuery Loves Developers - Oredev 2009

$(document).ready(function () { $(‘li’).addClass(‘item’);});

app.js

Page 64: jQuery Loves Developers - Oredev 2009

$(function () { $(‘li’).addClass(‘item’);});

app.js

Page 65: jQuery Loves Developers - Oredev 2009

jQuery(function ($) { $(‘li’).addClass(‘item’);});

app.js

Page 66: jQuery Loves Developers - Oredev 2009

Tip

Include jQuery after styles

Page 67: jQuery Loves Developers - Oredev 2009

jQuery API overview

Page 68: jQuery Loves Developers - Oredev 2009

• Core

• Selectors

• Attributes

• Traversing

• Manipulation

• CSS

• Events

• Effects

• Ajax

• Utilities

Page 69: jQuery Loves Developers - Oredev 2009

• Selectors

• Attributes

• Traversing

• Manipulation

• CSS

• Events

• Effects

• Ajax

• Utilities

jQuery()

each() size()eq() get()index()

lengthselectorcontext

data()removeData()queue()dequeue()

jQuery.fn.extend() jQuery.extend()jQuery.noConflict()

• Core

Page 70: jQuery Loves Developers - Oredev 2009

• Selectors

• Attributes

• Traversing

• Manipulation

• CSS

• Events

• Effects

• Ajax

• Utilities

jQuery()

each() size()eq() get()index()

lengthselectorcontext

data()removeData()queue()dequeue()

jQuery.fn.extend() jQuery.extend()jQuery.noConflict()

• Core

Page 71: jQuery Loves Developers - Oredev 2009

• Selectors

• Attributes

• Traversing

• Manipulation

• CSS

• Events

• Effects

• Ajax

• Utilities

<!DOCTYPE html><html><body>

<p>Element Node</p>

<script src="jquery.js"></script><script>alert($('p').get(0).nodeName);</script>

</body></html>

• Core

http://jsbin.com/ibito/edit

Page 72: jQuery Loves Developers - Oredev 2009

• Selectors

• Attributes

• Traversing

• Manipulation

• CSS

• Events

• Effects

• Ajax

• Utilities

<!DOCTYPE html><html><body>

<p>Element Node</p>

<script src="jquery.js"></script><script>alert($('p').get(0).nodeName);alert($('p')[0].nodeName);</script>

</body></html>

• Core

http://jsbin.com/idiyi/edit

Page 73: jQuery Loves Developers - Oredev 2009

• Core

• Attributes

• Traversing

• Manipulation

• CSS

• Events

• Effects

• Ajax

• Utilities

• Selectors

Page 74: jQuery Loves Developers - Oredev 2009

• Core

• Attributes

• Traversing

• Manipulation

• CSS

• Events

• Effects

• Ajax

• Utilities

• Selectors

$(‘#nav li.contact’)

Page 75: jQuery Loves Developers - Oredev 2009

• Core

• Attributes

• Traversing

• Manipulation

• CSS

• Events

• Effects

• Ajax

• Utilities

• Selectors

$(‘#nav li.contact’)

$(‘:visible’)

Page 76: jQuery Loves Developers - Oredev 2009

• Core

• Attributes

• Traversing

• Manipulation

• CSS

• Events

• Effects

• Ajax

• Utilities

• Selectors

$(‘#nav li.contact’)

$(‘:visible’)

$(‘:radio:enabled:checked’)

Page 77: jQuery Loves Developers - Oredev 2009

• Core

• Attributes

• Traversing

• Manipulation

• CSS

• Events

• Effects

• Ajax

• Utilities

• Selectors

$(‘#nav li.contact’)

$(‘:visible’)

$(‘:radio:enabled:checked’)

$(‘a[title]’)

Page 78: jQuery Loves Developers - Oredev 2009

• Core

• Attributes

• Traversing

• Manipulation

• CSS

• Events

• Effects

• Ajax

• Utilities

• Selectors

$(‘#nav li.contact’)

$(‘:visible’)

$(‘:radio:enabled:checked’)

$(‘a[title]’)

$(‘a[title][hash*=”foo”]’)

Page 79: jQuery Loves Developers - Oredev 2009

• Core

• Attributes

• Traversing

• Manipulation

• CSS

• Events

• Effects

• Ajax

• Utilities

• Selectors

$(‘#nav li.contact’)

$(‘:visible’)

$(‘:radio:enabled:checked’)

$(‘a[title]’)

$(‘a[title][hash*=”foo”]’)

$(‘a:first[hash*=”foo”]’)

Page 80: jQuery Loves Developers - Oredev 2009

• Core

• Attributes

• Traversing

• Manipulation

• CSS

• Events

• Effects

• Ajax

• Utilities

• Selectors

$(‘#nav li.contact’)

$(‘:visible’)

$(‘:radio:enabled:checked’)

$(‘a[title]’)

$(‘a[title][hash*=”foo”]’)

$(‘a:first[hash*=”foo”]’)

$(‘.header, .footer’)

Page 81: jQuery Loves Developers - Oredev 2009

• Core

• Attributes

• Traversing

• Manipulation

• CSS

• Events

• Effects

• Ajax

• Utilities

• Selectors

$(‘#nav li.contact’)

$(‘:visible’)

$(‘:radio:enabled:checked’)

$(‘a[title]’)

$(‘a[title][hash*=”foo”]’)

$(‘a:first[hash*=”foo”]’)

$(‘.header, .footer’)

$(‘.header, .footer’, ‘#main’)

Page 82: jQuery Loves Developers - Oredev 2009

getElementById

getElementsByTagName

getElementsByClassName

querySelectorAll

Insidetip Performance

Page 83: jQuery Loves Developers - Oredev 2009

getElementById

getElementsByTagName

getElementsByClassName

querySelectorAll

Insidetip Performance

jQuery selectors

fail silently,

just like CSS does!

Page 84: jQuery Loves Developers - Oredev 2009

• Core

• Selectors

• Attributes

• Manipulation

• CSS

• Events

• Effects

• Ajax

• Utilities

• Traversing

add()children()closest()contents()find()next()nextAll()offsetParent()parent() parents() prev() prevAll() siblings()

andSelf()end()

eq()filter()is()map()not()slice()

Page 85: jQuery Loves Developers - Oredev 2009

• Core

• Selectors

• Attributes

• Manipulation

• CSS

• Events

• Effects

• Ajax

• Utilities

• Traversing

add()children()closest()contents()find()next()nextAll()offsetParent()parent() parents() prev() prevAll() siblings()

andSelf()end()

eq()filter()is()map()not()slice()

New in

1.3

Page 86: jQuery Loves Developers - Oredev 2009

• Core

• Selectors

• Attributes

• Manipulation

• CSS

• Events

• Effects

• Ajax

• Utilities

• Traversing

add()children()closest()contents()find()next()nextAll()offsetParent()parent() parents() prev() prevAll() siblings()

andSelf()end()

eq()filter()is()map()not()slice()

Searches down

Page 87: jQuery Loves Developers - Oredev 2009

• Core

• Selectors

• Attributes

• Manipulation

• CSS

• Events

• Effects

• Ajax

• Utilities

• Traversing

add()children()closest()contents()find()next()nextAll()offsetParent()parent() parents() prev() prevAll() siblings()

andSelf()end()

eq()filter()is()map()not()slice()

Filters across

Page 88: jQuery Loves Developers - Oredev 2009

• Core

• Selectors

• Attributes

• Manipulation

• CSS

• Events

• Effects

• Ajax

• Utilities

• Traversing

add()children()closest()contents()find()next()nextAll()offsetParent()parent() parents() prev()prevAll() siblings()

andSelf()end()

eq()filter()is()map()not()slice()

Page 89: jQuery Loves Developers - Oredev 2009

• Core

• Selectors

• Attributes

• Manipulation

• CSS

• Events

• Effects

• Ajax

• Utilities

• Traversing

<!DOCTYPE html><html><body><ul> <li><a>one</a></li> <li><a>two</a></li> <li><a>three</a></li></ul><script src="jquery.js"></script><script>$('a').click(function () { alert( $(this) .parent() .prevAll() .length )});</script></body></html>http://jsbin.com/ubuhe/edit

Page 90: jQuery Loves Developers - Oredev 2009

• Core

• Selectors

• Attributes

• Traversing

• Manipulation

• CSS

• Effects

• Ajax

• Utilities

• Events

ready()

bind()one()trigger()triggerHandler()unbind()

live()die()

hover()toggle()

load()unload()error()

blur()change()click()dbclick()focus()keydown()keypress()keyup()mousedown()mousenter()mouseleave()mouseout()mouseup()resize()scroll()select()submit()

Page 91: jQuery Loves Developers - Oredev 2009

• Core

• Selectors

• Attributes

• Traversing

• Manipulation

• CSS

• Effects

• Ajax

• Utilities

• Events

ready()

bind()one()trigger()triggerHandler()unbind()

live()die()

hover()toggle()

load()unload()error()

blur()change()click()dbclick()focus()keydown()keypress()keyup()mousedown()mousenter()mouseleave()mouseout()mouseup()resize()scroll()select()submit()

IE fires on blur

Page 92: jQuery Loves Developers - Oredev 2009

• Core

• Selectors

• Attributes

• Traversing

• Manipulation

• CSS

• Effects

• Ajax

• Utilities

• Events

ready()

bind()one()trigger()triggerHandler()unbind()

live()die()

hover()toggle()

load()unload()error()

blur()change()click()dbclick()focus()keydown()keypress()keyup()mousedown()mousenter()mouseleave()mouseout()mouseup()resize()scroll()select()submit()

Page 93: jQuery Loves Developers - Oredev 2009

<!DOCTYPE html><html><body>

<p>1. click</p><p>2. click</p>

<script src="jquery.js"></script><script>

$(‘p’).bind(‘click’, function(){ $(this).after(‘<p>’ + ➥ $(this).text()+‘ clicked</p>’); });

</script></body></html>

• Core

• Selectors

• Attributes

• Traversing

• Manipulation

• CSS

• Effects

• Ajax

• Utilities

• Events

http://jsbin.com/ededi/edit

Page 94: jQuery Loves Developers - Oredev 2009

• Core

• Selectors

• Attributes

• Traversing

• Manipulation

• CSS

• Effects

• Ajax

• Utilities

• Events

<!DOCTYPE html><html><body>

<p>1. click</p><p>2. click</p>

<script src="jquery.js"></script><script>

$(‘p’).live(‘click’, function(){ $(this).after(‘<p>’ + ➥ $(this).text()+‘ clicked</p>’); });

</script></body></html> http://jsbin.com/ihalu/edit

Page 95: jQuery Loves Developers - Oredev 2009

• Core

• Selectors

• Attributes

• Traversing

• Manipulation

• CSS

• Events

• Ajax

• Utilities

• Effects

$.fx.off

show()hide()toggle()

slideDown()slideUp()slideToggle()

fadeIn()fadeOut()fadeTo()

animate()stop()

Page 96: jQuery Loves Developers - Oredev 2009

• Core

• Selectors

• Attributes

• Traversing

• Manipulation

• CSS

• Events

• Ajax

• Utilities

• Effects

$.fx.off

show()hide()toggle()

slideDown()slideUp()slideToggle()

fadeIn()fadeOut()fadeTo()

animate()stop()

Blanket FX control New in

1.3

Page 97: jQuery Loves Developers - Oredev 2009

• Core

• Selectors

• Attributes

• Traversing

• Manipulation

• CSS

• Events

• Ajax

• Utilities

• Effects

$.fx.off

show()hide()toggle()

slideDown()slideUp()slideToggle()

fadeIn()fadeOut()fadeTo()

animate()stop()

Page 98: jQuery Loves Developers - Oredev 2009

• Core

• Selectors

• Attributes

• Traversing

• Manipulation

• CSS

• Events

• Ajax

• Utilities

• Effects

<!DOCTYPE html><html><head><style> div {background:#bca; width:100px;border:1px solid green;}</style></head><body><div id="block">Hello!</div><script src="jquery.js"></script><script>

$(‘#block’).animate({ ! width: ‘70%’,! opacity: 0.4,! marginLeft: ‘0.6in’,! fontSize: ‘3em’,! borderWidth: ‘10px’}, 1500);

</script></body></html>

http://jsbin.com/oroto/edit

Page 99: jQuery Loves Developers - Oredev 2009
Page 100: jQuery Loves Developers - Oredev 2009

• Core

• Selectors

• Attributes

• Traversing

• Manipulation

• CSS

• Events

• Effects

• Utilities

• Ajax

$.ajax()$.get()$.post()$.getJSON() $.getScript()$.ajaxSetup()

load()

serialize()serializeArray()

ajaxCompete()ajaxError()ajaxSend()ajaxStart()ajaxStop()ajaxSuccess()

Page 101: jQuery Loves Developers - Oredev 2009

• Core

• Selectors

• Attributes

• Traversing

• Manipulation

• CSS

• Events

• Effects

• Utilities

• Ajax

$.ajax()$.get()$.post()$.getJSON() $.getScript()$.ajaxSetup()

load()

serialize()serializeArray()

ajaxCompete()ajaxError()ajaxSend()ajaxStart()ajaxStop()ajaxSuccess()

Page 102: jQuery Loves Developers - Oredev 2009

• Core

• Selectors

• Attributes

• Traversing

• Manipulation

• CSS

• Events

• Effects

• Utilities

• Ajax

<!DOCTYPE html><html><body> <script src="jquery.js"></script> <script>

$.getJSON("http://twitter.com/statuses/user_timeline.json?screen_name=rem&callback=?",function(data){ $.each(data,function(i,tweet){ $('<p/>').html(tweet.text).appendTo('body'); if ( i == 30 ) return false; }); }); </script></body></html>

http://jsbin.com/acisi/edit

Page 103: jQuery Loves Developers - Oredev 2009

Plugins

Page 104: jQuery Loves Developers - Oredev 2009

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 105: jQuery Loves Developers - Oredev 2009

Plugin design in 6 steps

Page 106: jQuery Loves Developers - Oredev 2009

Step 1:

create a private scope for $ alias

Page 107: jQuery Loves Developers - Oredev 2009

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

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

Page 108: jQuery Loves Developers - Oredev 2009

Step 2:

attach plugin to fn alias (aka prototype)

Page 109: jQuery Loves Developers - Oredev 2009

<!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 110: jQuery Loves Developers - Oredev 2009

<!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 111: jQuery Loves Developers - Oredev 2009

Step 3:

add implicit iteration

Page 112: jQuery Loves Developers - Oredev 2009

<!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 113: jQuery Loves Developers - Oredev 2009

Step 4:

enable chaining

Page 114: jQuery Loves Developers - Oredev 2009

<!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 115: jQuery Loves Developers - Oredev 2009

<!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 116: jQuery Loves Developers - Oredev 2009

<!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 117: jQuery Loves Developers - Oredev 2009

Step 5:

add default options

Page 118: jQuery Loves Developers - Oredev 2009

<!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 119: jQuery Loves Developers - Oredev 2009

Step 6:

add custom options

Page 120: jQuery Loves Developers - Oredev 2009

<!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 121: jQuery Loves Developers - Oredev 2009

<!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 122: jQuery Loves Developers - Oredev 2009

<!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 123: jQuery Loves Developers - Oredev 2009

<!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 124: jQuery Loves Developers - Oredev 2009
Page 125: jQuery Loves Developers - Oredev 2009

News

• Four conferences next year: London, Boston, San Francisco and online

• New plugin site

• jQuery Forum (moving from Google Groups)

• Mobile jQuery

Page 126: jQuery Loves Developers - Oredev 2009

Demo!

Page 127: jQuery Loves Developers - Oredev 2009

Remy Sharp @rem

jQuery team member

Co-author of O'Reilly jQuery Cookbook(due November 20th)

jqueryfordesigners.com

remysharp.com

Page 128: jQuery Loves Developers - Oredev 2009

Remy Sharp @rem

jQuery team member

Co-author of O'Reilly jQuery Cookbook(due November 20th)

jqueryfordesigners.com

remysharp.com

?Questions