write less do more

136

Upload: remy-sharp

Post on 17-May-2015

6.616 views

Category:

Technology


0 download

DESCRIPTION

London and Cambridge jQuery DevDays talk introducing jQuery concepts, API overview, live examples and plugin design.

TRANSCRIPT

Page 1: Write Less Do More
Page 2: Write Less Do More

Overview

• Who, what and why of jQuery

• 5 core jQuery concepts

• Overview of jQuery API

• Building a plugin in 6 steps

• jQuery News

Page 3: Write Less Do More

Who's using jQuery

Page 4: Write Less Do More

Who's using jQuery

Page 5: Write Less Do More

Who's using jQuery

reddit.comespn.comibm.com

boxee.tvbit.ly

twitpic.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

stackoverflow.com

Page 6: Write Less Do More

Who's using jQuery

reddit.comespn.comibm.com

boxee.tvbit.ly

twitpic.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

stackoverflow.com

Page 7: Write Less Do More

What exactly is jQuery

• Dealing with the DOM(e.g. selecting, creating, traversing, changing, etc)

• JavaScript Events

• Animations

• Ajax interactions

jQuery is a JavaScript library!

Page 8: Write Less Do More

What does that mean?

Page 9: Write Less Do More

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: Write Less Do More

jQuery simpli!es

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

Page 11: Write Less Do More

jQuery simpli!es

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

jQuery function

Page 12: Write Less Do More

jQuery simpli!es

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

jQuery function

CSS expression

Page 13: Write Less Do More

jQuery simpli!es

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

jQuery function

CSS expression

jQuery method

Page 14: Write Less Do More

jQuery simpli!es

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

Page 15: Write Less Do More

Write less, do more

It really is the

JavaScript library!

Page 16: Write Less Do More

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: Write Less Do More

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 18: Write Less Do More

Help!

Page 19: Write Less Do More

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: Write Less Do More

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 21: Write Less Do More

Concept 1:

knowing the jQuery parameter types

Page 22: Write Less Do More

• CSS selectors & custom CSS expressions

• HTML

• DOM elements

• A function (shortcut for DOM ready)

Page 23: Write Less Do More

• CSS selectors & custom CSS expressions

• HTML

• DOM elements

• A function (shortcut for DOM ready)

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

Page 24: Write Less Do More

• 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 25: Write Less Do More

• 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 26: Write Less Do More

• 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: Write Less Do More

• 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 28: Write Less Do More

Concept 2:

!nd something,do something

Page 29: Write Less Do More

<!DOCTYPE html><html><body> <ul> <li><a>home</a></li> <li><a>about</a></li> </ul></body></html>

Page 30: Write Less Do More

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

Page 31: Write Less Do More

<!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('ul').attr('id', 'nav');</script></body></html>

Page 32: Write Less Do More

<!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 a');</script></body></html>

Page 33: Write Less Do More

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

Page 34: Write Less Do More

Concept 3:

create something,do something

Page 35: Write Less Do More

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

Page 36: Write Less Do More

<!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 37: Write Less Do More

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

Page 38: Write Less Do More

<!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(‘<li>home</li>’)➥.wrapInner(‘<a/>’).appendTo(‘#nav’); jQuery(‘<li>about</li>’)➥.wrapInner(‘<a/>’).appendTo(‘#nav’);</script></body></html>

Page 39: Write Less Do More

Concept 4:

chaining & operating

Page 40: Write Less Do More

<!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 41: Write Less Do More

<!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 42: Write Less Do More

<!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 43: Write Less Do More

<!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 44: Write Less Do More

<!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 45: Write Less Do More

<!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 46: Write Less Do More

Concept 5:

understanding implicit iteration

Page 47: Write Less Do More

<!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: Write Less Do More

<!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: Write Less Do More

• Knowing the jQuery parameter types

• Find something, do something

• Create something, do something

• Chaining & Operating

• Understanding Implicit Iteration

Review

Page 50: Write Less Do More

“What about the bling function?”

Page 51: Write Less Do More

jQuery == $

Page 52: Write Less Do More

$ == jQuery

Page 53: Write Less Do More

$ is an alias to jQuery

Page 54: Write Less Do More

<!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: Write Less Do More

<!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: Write Less Do More

<!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: Write Less Do More

<!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: Write Less Do More

<!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 59: Write Less Do More

<!DOCTYPE html><html><head></head><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 60: Write Less Do More

<!DOCTYPE html><html><head></head><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: Write Less Do More

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

Page 62: Write Less Do More

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

Page 63: Write Less Do More

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

Page 64: Write Less Do More

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

Page 65: Write Less Do More

jQuery API overview

Page 66: Write Less Do More

• Core

• Selectors

• Attributes

• Traversing

• Manipulation

• CSS

• Events

• Effects

• Ajax

• Utilities

Page 67: Write Less Do More

• 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 68: Write Less Do More

• 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 69: Write Less Do More

• 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 70: Write Less Do More

• 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 71: Write Less Do More

• Core

• Attributes

• Traversing

• Manipulation

• CSS

• Events

• Effects

• Ajax

• Utilities

• Selectors

Page 72: Write Less Do More

• Core

• Attributes

• Traversing

• Manipulation

• CSS

• Events

• Effects

• Ajax

• Utilities

• Selectors

$(‘#nav li.contact’)

Page 73: Write Less Do More

• Core

• Attributes

• Traversing

• Manipulation

• CSS

• Events

• Effects

• Ajax

• Utilities

• Selectors

$(‘#nav li.contact’)

$(‘:visible’)

Page 74: Write Less Do More

• Core

• Attributes

• Traversing

• Manipulation

• CSS

• Events

• Effects

• Ajax

• Utilities

• Selectors

$(‘#nav li.contact’)

$(‘:visible’)

$(‘:radio:enabled:checked’)

Page 75: Write Less Do More

• Core

• Attributes

• Traversing

• Manipulation

• CSS

• Events

• Effects

• Ajax

• Utilities

• Selectors

$(‘#nav li.contact’)

$(‘:visible’)

$(‘:radio:enabled:checked’)

$(‘a[title]’)

Page 76: Write Less Do More

• Core

• Attributes

• Traversing

• Manipulation

• CSS

• Events

• Effects

• Ajax

• Utilities

• Selectors

$(‘#nav li.contact’)

$(‘:visible’)

$(‘:radio:enabled:checked’)

$(‘a[title]’)

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

Page 77: Write Less Do More

• 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 78: Write Less Do More

• 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 79: Write Less Do More

• 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 80: Write Less Do More

• 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’)

http://codylindley.com/jqueryselectors

Page 82: Write Less Do More

• Core

• Selectors

• Traversing

• Manipulation

• CSS

• Events

• Effects

• Ajax

• Utilities

• Attributes

attr()removeAttr()

addClass()hasClass()toggleClass()removeClass()

val()

Page 83: Write Less Do More

• Core

• Selectors

• Traversing

• Manipulation

• CSS

• Events

• Effects

• Ajax

• Utilities

• Attributes

attr()removeAttr()

addClass()hasClass()toggleClass()removeClass()

val()

Page 84: Write Less Do More

• Core

• Selectors

• Traversing

• Manipulation

• CSS

• Events

• Effects

• Ajax

• Utilities

• Attributes

<!DOCTYPE html><html><body>

<input type="text" value="search">

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

$('input').focus(function(){ var v = $(this).val(); $(this).val( v === this.defaultValue ? '' : v );}).blur(function(){ var v = $(this).val(); $(this).val( $.trim(v) ? v : this.defaultValue ); });

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

Page 85: Write Less Do More

• 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 86: Write Less Do More

• 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: Write Less Do More

• 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: Write Less Do More

• 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: Write Less Do More

• Core

• Selectors

• Attributes

• Manipulation

• CSS

• Events

• Effects

• Ajax

• Utilities

• Traversing

<!DOCTYPE html><html><body>

<p>Make me bold!</p>

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

$('p') .contents() .wrap('<strong />');

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

http://jsbin.com/owesu/edit

Page 90: Write Less Do More

• Core

• Selectors

• Attributes

• Traversing

• CSS

• Events

• Effects

• Ajax

• Utilities

• Manipulation

html()text()

append()appendTo()prepend()prependTo()

after()before()insert()insertAfter()insertBefore

wrap()wrapAll()wrapInner()

replaceWith()replaceAll()

empty()remove()

clone()

Page 91: Write Less Do More

• Core

• Selectors

• Attributes

• Traversing

• CSS

• Events

• Effects

• Ajax

• Utilities

• Manipulation

html()text()

append()appendTo()prepend()prependTo()

after()before()insert()insertAfter()insertBefore

wrap()wrapAll()wrapInner()

replaceWith()replaceAll()

empty()remove()

clone()

Page 92: Write Less Do More

• Core

• Selectors

• Attributes

• Traversing

• CSS

• Events

• Effects

• Ajax

• Utilities

• Manipulation

<!DOCTYPE html><html><body>

<p>jQuery’s <em>easy!</em></p>

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

alert($(‘p’).text());

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

http://jsbin.com/inulu/edit

Page 93: Write Less Do More

• Core

• Selectors

• Attributes

• Traversing

• Manipulation

• Events

• Effects

• Ajax

• Utilities

• CSS

css()

offset()offsetParent()position()scrollTop()scrollLeft()

height()width()innerHeight()innerWidth()outerHeight()outerWidth()

Page 94: Write Less Do More

• Core

• Selectors

• Attributes

• Traversing

• Manipulation

• Events

• Effects

• Ajax

• Utilities

• CSS

css()

offset()offsetParent()position()scrollTop()scrollLeft()

height()width()innerHeight()innerWidth()outerHeight()outerWidth()

Page 95: Write Less Do More

• Core

• Selectors

• Attributes

• Traversing

• Manipulation

• Events

• Effects

• Ajax

• Utilities

• CSS

<!DOCTYPE html><html><head><style>div { background: #ccc; width: 100px; margin: 0 20px; float: left; }</style></head><body>

<div></div><div></div><div></div>

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

$('div').height($(document).height());

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

Page 96: Write Less Do More

• 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 97: Write Less Do More

• 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()

Acts on first, and doesn’t chain!

Page 98: Write Less Do More

• 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 99: Write Less Do More

• 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 100: Write Less Do More

• 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’).bind(‘click’, function(){ $(this).after(‘<p>clicked</p>’); });

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

http://jsbin.com/ogahu/edit

Page 101: Write Less Do More

• 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 102: Write Less Do More

• Core

• Selectors

• Attributes

• Traversing

• Manipulation

• CSS

• Events

• Ajax

• Utilities

• Effects

show()hide()toggle()

slideDown()slideUp()slideToggle()

fadeIn()fadeOut()fadeTo()

animate()stop()

Page 103: Write Less Do More

• Core

• Selectors

• Attributes

• Traversing

• Manipulation

• CSS

• Events

• Ajax

• Utilities

• Effects

show()hide()toggle()

slideDown()slideUp()slideToggle()

fadeIn()fadeOut()fadeTo()

animate()stop()

Page 104: Write Less Do More

• 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 105: Write Less Do More

• 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 106: Write Less Do More

• 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 107: Write Less Do More

• 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 108: Write Less Do More

• Core

• Selectors

• Attributes

• Traversing

• Manipulation

• CSS

• Events

• Effects

• Ajax

• Utilities

$.support$.boxModel$.browser

$.each()$.extend()$.grep()$.makeArray()$.map()$.inArray()$.merge()$.unique()

$.isArray()$.isFunction()

$.trim()$.param()

Page 109: Write Less Do More

• Core

• Selectors

• Attributes

• Traversing

• Manipulation

• CSS

• Events

• Effects

• Ajax

• Utilities

$.support$.boxModel$.browser

$.each()$.extend()$.grep()$.makeArray()$.map()$.inArray()$.merge()$.unique()

$.isArray()$.isFunction()

$.trim()$.param()

Page 110: Write Less Do More

• Core

• Selectors

• Attributes

• Traversing

• Manipulation

• CSS

• Events

• Effects

• Ajax

• Utilities

$.each(data, function(i, tweet){ $('<p />') .html(tweet.text) .appendTo('body');

if ( i == 30 ) { return false; }});

http://jsbin.com/acisi/edit

Page 111: Write Less Do More

Plugins

Page 112: Write Less Do More

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 113: Write Less Do More

Why create a plugin?

The “do something” isn’t available in jQuery

Page 114: Write Less Do More

Why create a plugin?

Roll your own “do something” with a plugin!

Page 115: Write Less Do More

A plugin in 6 steps

Page 116: Write Less Do More

Step 1:

create a private scope for $ alias

Page 117: Write Less Do More

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

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

Page 118: Write Less Do More

Step 2:

attach plugin to fn alias (aka prototype)

Page 119: Write Less Do More

<!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 120: Write Less Do More

<!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 121: Write Less Do More

Step 3:

add implicit iteration

Page 122: Write Less Do More

<!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 123: Write Less Do More

Step 4:

enable chaining

Page 124: Write Less Do More

<!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 125: Write Less Do More

Step 5:

add default options

Page 126: Write Less Do More

<!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 127: Write Less Do More

Step 6:

add custom options

Page 128: Write Less Do More

<!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 129: Write Less Do More

<!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 130: Write Less Do More

<!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 131: Write Less Do More

<!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 132: Write Less Do More
Page 133: Write Less Do More

Plugins are simple, just follow those steps.

Page 134: Write Less Do More

News• Four conferences next year:

London, Boston, San Francisco and online

• New plugin site

• jQuery Forum (moving from Google Groups)

• jQuery.org & Foundation (Software Freedom Law Centre)

• Infrastructure upgrade

Page 135: Write Less Do More

Remy Sharp @rem

jQuery team member

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

jqueryfordesigners.com

remysharp.com

Special thanks to Cody Lindley

Page 136: Write Less Do More

Remy Sharp @rem

jQuery team member

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

jqueryfordesigners.com

remysharp.com

?Questions

Special thanks to Cody Lindley