jQuery Loves Developers - Oredev 2009
Upcoming SlideShare
Loading in...5
×
 

jQuery Loves Developers - Oredev 2009

on

  • 28,421 views

jQuery is a JavaScript library which allows you to develop solutions with less code, in less time. You can build interactive prototypes for your prospective clients, or take an existing solution and ...

jQuery is a JavaScript library which allows you to develop solutions with less code, in less time. You can build interactive prototypes for your prospective clients, or take an existing solution and add new dynamic behaviour with little effort.

We will see how jQuery can be used to quickly and concisely apply JavaScript behaviour to your web app. It will cover selectors, Ajax, DOM manipulation and more. The aim: to produce lean unobtrusive JavaScript with jQuery.

Statistics

Views

Total Views
28,421
Slideshare-icon Views on SlideShare
7,579
Embed Views
20,842

Actions

Likes
20
Downloads
541
Comments
5

20 Embeds 20,842

http://web.enavu.com 19191
http://tutsvalley.com 1415
http://translate.googleusercontent.com 75
http://onwebdev.blogspot.com 72
http://www.mysrc.de 54
http://lanyrd.com 8
http://www.slideshare.net 7
http://onwebdev.blogspot.co.uk 4
http://onwebdev.blogspot.in 3
http://207.46.192.232 2
http://l.lj-toys.com 2
http://xss.yandex.net 1
http://docs.jquery.com 1
http://static.slidesharecdn.com 1
http://131.253.14.250 1
http://onwebdev.blogspot.com.br 1
http://password-prog.blogspot.com 1
http://localhost 1
http://onwebdev.blogspot.co.nz 1
http://fanyi.youdao.com 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

15 of 5 Post a comment

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    jQuery Loves Developers - Oredev 2009 jQuery Loves Developers - Oredev 2009 Presentation Transcript

    • jQuery <3 Developers
    • Overview • Who, what, why of jQuery • Core concepts of jQuery • API overview & tips • Plugin design pattern • News • Live demo
    • Quick survey first
    • Who?
    • Who?
    • Who? reddit.com whitehouse.gov overstock.com espn.com microsoft.com time.com ibm.com amazon.com capitalone.com stackoverflow.com netflix.com wordpress.com boxee.tv bing.com dell.com bit.ly monster.com twitter.com twitpic.com tv.com w3.org http://trends.builtwith.com/javascript/jquery
    • What? jQuery is a JavaScript library. • Selector engine: Sizzle • DOM manipulation • Events • Effects • Ajax
    • What does that mean?
    • 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'; } } }
    • jQuery simpli es jQuery('table tr:nth-child(odd)').addClass('odd');
    • jQuery simpli es jQuery function jQuery('table tr:nth-child(odd)').addClass('odd');
    • jQuery simpli es jQuery function jQuery('table tr:nth-child(odd)').addClass('odd'); CSS expression
    • jQuery simpli es jQuery function jQuery method jQuery('table tr:nth-child(odd)').addClass('odd'); CSS expression
    • jQuery simpli es jQuery('table tr:nth-child(odd)').addClass('odd');
    • 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)
    • 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)
    • Help!
    • APIs Blogs, tutorials, screencasts, docs.jquery.com plugins, development sprints api.jquery.com visualjquery.com Google Groups jquery-en Twitter jquery-dev @jquery Help! jquery-ui-en @jquerysites jquery-ui-dev @jqueryui jquery-a11y IRC channel irc.freenode.net/#jquery
    • APIs Blogs, tutorials, screencasts, docs.jquery.com plugins, development sprints api.jquery.com visualjquery.com Google Groups jquery-en Twitter jquery-dev @jquery Help! jquery-ui-en @jquerysites jquery-ui-dev @jqueryui jquery-a11y IRC channel irc.freenode.net/#jquery
    • Concept 1: knowing the jQuery parameter types
    • • CSS selectors & custom CSS expressions • HTML • DOM elements • A function (shortcut for DOM ready)
    • jQuery(‘div’) & jQuery(‘:first’) • CSS selectors & custom CSS expressions • HTML • DOM elements • A function (shortcut for DOM ready)
    • jQuery(‘<li><a href=”#”>link</a></li>’) • CSS selectors jQuery(‘:first’) jQuery(‘div’) & & custom CSS expressions • HTML • DOM elements • A function (shortcut for DOM ready)
    • jQuery(document) or jQuery(document.createElement(‘a’)) • CSS selectors jQuery(‘:first’) jQuery(‘div’) & & custom CSS expressions • HTML jQuery(‘<li><a href=”#”>link</a></li>’) • DOM elements • A function (shortcut for DOM ready)
    • jQuery(function(){}) = jQuery(document).ready(function(){}) • CSS selectors jQuery(‘:first’) jQuery(‘div’) & & custom CSS expressions • HTML jQuery(‘<li><a href=”#”>link</a></li>’) • DOM elements jQuery(document) or jQuery(document.createElement(‘a’)) • A function (shortcut for DOM ready)
    • • CSS selectors jQuery(‘:first’) jQuery(‘div’) & & custom CSS expressions • HTML jQuery(‘<li><a href=”#”>link</a></li>’) • DOM elements jQuery(document) or jQuery(document.createElement(‘a’)) • A function (shortcut for DOM ready) jQuery(function(){}) = jQuery(document).ready(function(){})
    • Concept 2: nd something, do something
    • <!DOCTYPE html> <html> <body> <ul id="nav"> <li><a>home</a></li> <li><a>about</a></li> </ul> </body> </html>
    • <!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>
    • <!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>
    • <!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>
    • Concept 3: create something, do something
    • <!DOCTYPE html> <html> <body> <ul id='nav'> </ul> </body> </html>
    • <!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>
    • <!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>
    • Insi de tip createDocumentFragment
    • Insi de tip createDocumentFragment (IE6 || IE7) && innerHTML && HTML5 == fail
    • Concept 4: chaining & operating
    • <!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>
    • <!DOCTYPE html> <html> 1 <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> 1 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>
    • <!DOCTYPE html> <html> 1 <body> <ul id='nav'> <li class=”item”><a href=”/home”>home</a></li> <li class=”item”><a href=”/about”>about</a></li> </ul> 2 <script src=”jquery.js”></script> <script> 1 jQuery(‘ul’).attr(‘id’, ‘nav’); 2 jQuery(‘#nav li’).addClass(‘item’); jQuery(‘#nav a’).each(function () { jQuery(this).attr(‘href’, ‘/’+jQuery(this).text()); }); </script> </body> </html>
    • <!DOCTYPE html> <html> 1 <body> <ul id='nav'> 3 <li class=”item”><a href=”/home”>home</a></li> <li class=”item”><a href=”/about”>about</a></li> </ul> 2 <script src=”jquery.js”></script> <script> 1 jQuery(‘ul’).attr(‘id’, ‘nav’); 2 jQuery(‘#nav li’).addClass(‘item’); jQuery(‘#nav a’).each(function () { jQuery(this).attr(‘href’, ‘/’+jQuery(this).text()); }); </script> 3 </body> </html>
    • <!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>
    • <!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>
    • jQuery returns itself containing the current DOM collection
    • Concept 5: understanding implicit iteration
    • <!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>
    • <!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>
    • Review • Knowing the jQuery parameter types • Find something, do something • Create something, do something • Chaining & Operating • Understanding Implicit Iteration
    • “What about the bling function?”
    • jQuery == $
    • $ == jQuery
    • $ is an alias to jQuery
    • <!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>
    • <!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>
    • <!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>
    • <!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>
    • More than ready
    • <!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>
    • <!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>
    • <!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>
    • app.js $(document).ready(function () { $(‘li’).addClass(‘item’); });
    • app.js $(document).ready(function () { $(‘li’).addClass(‘item’); });
    • app.js $(function () { $(‘li’).addClass(‘item’); });
    • app.js jQuery(function ($) { $(‘li’).addClass(‘item’); });
    • Tip Include jQuery after styles
    • jQuery API overview
    • • Core • Selectors • Attributes • Traversing • Manipulation • CSS • Events • Effects • Ajax • Utilities
    • • Core jQuery() • Selectors each() size() • Attributes eq() get() • Traversing index() • Manipulation length selector • CSS context • Events data() removeData() • Effects queue() dequeue() • Ajax jQuery.fn.extend() • Utilities jQuery.extend() jQuery.noConflict()
    • • Core jQuery() • Selectors each() size() • Attributes eq() get() • Traversing index() • Manipulation length selector • CSS context • Events data() removeData() • Effects queue() dequeue() • Ajax jQuery.fn.extend() • Utilities jQuery.extend() jQuery.noConflict()
    • • Core <!DOCTYPE html> • Selectors <html> <body> • Attributes <p>Element Node</p> • Traversing • <script src="jquery.js"> Manipulation </script> • CSS <script> alert($('p').get(0).nodeName); • Events </script> • Effects </body> • Ajax </html> • Utilities http://jsbin.com/ibito/edit
    • • Core <!DOCTYPE html> • Selectors <html> <body> • Attributes <p>Element Node</p> • Traversing • <script src="jquery.js"> Manipulation </script> • CSS <script> alert($('p').get(0).nodeName); • Events alert($('p')[0].nodeName); </script> • Effects • Ajax </body> </html> • Utilities http://jsbin.com/idiyi/edit
    • • Core • Selectors • Attributes • Traversing • Manipulation • CSS • Events • Effects • Ajax • Utilities
    • • Core $(‘#nav li.contact’) • Selectors • Attributes • Traversing • Manipulation • CSS • Events • Effects • Ajax • Utilities
    • • Core $(‘#nav li.contact’) • Selectors $(‘:visible’) • Attributes • Traversing • Manipulation • CSS • Events • Effects • Ajax • Utilities
    • • Core $(‘#nav li.contact’) • Selectors $(‘:visible’) • Attributes $(‘:radio:enabled:checked’) • Traversing • Manipulation • CSS • Events • Effects • Ajax • Utilities
    • • Core $(‘#nav li.contact’) • Selectors $(‘:visible’) • Attributes $(‘:radio:enabled:checked’) • Traversing $(‘a[title]’) • Manipulation • CSS • Events • Effects • Ajax • Utilities
    • • Core $(‘#nav li.contact’) • Selectors $(‘:visible’) • Attributes $(‘:radio:enabled:checked’) • Traversing $(‘a[title]’) • Manipulation $(‘a[title][hash*=”foo”]’) • CSS • Events • Effects • Ajax • Utilities
    • • Core $(‘#nav li.contact’) • Selectors $(‘:visible’) • Attributes $(‘:radio:enabled:checked’) • Traversing $(‘a[title]’) • Manipulation $(‘a[title][hash*=”foo”]’) • CSS $(‘a:first[hash*=”foo”]’) • Events • Effects • Ajax • Utilities
    • • Core $(‘#nav li.contact’) • Selectors $(‘:visible’) • Attributes $(‘:radio:enabled:checked’) • Traversing $(‘a[title]’) • Manipulation $(‘a[title][hash*=”foo”]’) • CSS $(‘a:first[hash*=”foo”]’) • Events $(‘.header, .footer’) • Effects • Ajax • Utilities
    • • Core $(‘#nav li.contact’) • Selectors $(‘:visible’) • Attributes $(‘:radio:enabled:checked’) • Traversing $(‘a[title]’) • Manipulation $(‘a[title][hash*=”foo”]’) • CSS $(‘a:first[hash*=”foo”]’) • Events $(‘.header, .footer’) • Effects $(‘.header, .footer’, ‘#main’) • Ajax • Utilities
    • Insi de tip Performance getElementById getElementsByTagName getElementsByClassName querySelectorAll
    • Insi de tip Performance ry s elec tors Que getElementById j fail s ilen tly, getElementsByTagName SS d oes! ju st l ike C getElementsByClassName querySelectorAll
    • • Core add() eq() • children() filter() Selectors closest() is() • Attributes contents() find() map() not() • Traversing next() nextAll() slice() • Manipulation offsetParent() parent() • CSS parents() prev() • Events prevAll() siblings() • Effects andSelf() • Ajax end() • Utilities
    • Ne•wCore in add() eq() • .3 children() filter() Selectors 1 Attributes • closest() contents() is() map() find() not() • Traversing next() nextAll() slice() • Manipulation offsetParent() parent() • CSS parents() prev() • Events prevAll() siblings() • Effects andSelf() • Ajax end() • Utilities
    • • Core add() eq() • Selectors children() filter() closest() is() • Searches down Attributes contents() find() map() not() • Traversing next() nextAll() slice() • Manipulation offsetParent() parent() • CSS parents() prev() • Events prevAll() siblings() • Effects andSelf() • Ajax end() • Utilities
    • • Core add() Filters across eq() • children() filter() Selectors closest() is() • Attributes contents() find() map() not() • Traversing next() nextAll() slice() • Manipulation offsetParent() parent() • CSS parents() prev() • Events prevAll() siblings() • Effects andSelf() • Ajax end() • Utilities
    • • Core add() eq() • children() filter() Selectors closest() is() • Attributes contents() find() map() not() • Traversing next() nextAll() slice() • Manipulation offsetParent() parent() • CSS parents() prev() • Events prevAll() siblings() • Effects andSelf() • Ajax end() • Utilities
    • • Core <!DOCTYPE html><html><body> • <ul> Selectors <li><a>one</a></li> • Attributes <li><a>two</a></li> <li><a>three</a></li> • Traversing </ul> <script src="jquery.js"> • Manipulation </script> <script> • CSS $('a').click(function () { alert( $(this) • Events .parent() .prevAll() • Effects ) .length • Ajax }); • </script> Utilities </body></html>http://jsbin.com/ubuhe/edit
    • • Core ready() blur() • change() Selectors bind() click() • Attributes one() trigger() dbclick() focus() • Traversing triggerHandler() keydown() unbind() keypress() • Manipulation live() keyup() mousedown() • CSS die() mousenter() mouseleave() • Events hover() toggle() mouseout() mouseup() • Effects load() resize() scroll() • Ajax unload() select() • error() submit() Utilities
    • • Core ready() blur() IE fires on blur • change() Selectors bind() click() • Attributes one() trigger() dbclick() focus() • Traversing triggerHandler() keydown() unbind() keypress() • Manipulation live() keyup() mousedown() • CSS die() mousenter() mouseleave() • Events hover() toggle() mouseout() mouseup() • Effects load() resize() scroll() • Ajax unload() select() • error() submit() Utilities
    • • Core ready() blur() • change() Selectors bind() click() • Attributes one() trigger() dbclick() focus() • Traversing triggerHandler() keydown() unbind() keypress() • Manipulation live() keyup() mousedown() • CSS die() mousenter() mouseleave() • Events hover() toggle() mouseout() mouseup() • Effects load() resize() scroll() • Ajax unload() select() • error() submit() Utilities
    • • Core <!DOCTYPE html> • <html> Selectors <body> • Attributes <p>1. click</p> • Traversing <p>2. click</p> • Manipulation <script src="jquery.js"></script> <script> • CSS $(‘p’).bind(‘click’, function(){ • Events $(this).after(‘<p>’ + ➥ $(this).text()+‘ clicked</p>’); • Effects }); • Ajax </script> </body> • Utilities </html> http://jsbin.com/ededi/edit
    • • Core <!DOCTYPE html> • <html> Selectors <body> • Attributes <p>1. click</p> • Traversing <p>2. click</p> • Manipulation <script src="jquery.js"></script> <script> • CSS $(‘p’).live(‘click’, function(){ • Events $(this).after(‘<p>’ + ➥ $(this).text()+‘ clicked</p>’); • Effects }); • Ajax </script> </body> • Utilities </html> http://jsbin.com/ihalu/edit
    • • Core $.fx.off • Selectors show() • Attributes hide() toggle() • Traversing slideDown() • Manipulation slideUp() • CSS slideToggle() • Events fadeIn() fadeOut() • Effects fadeTo() • Ajax animate() • Utilities stop()
    • • Core control ew in Blanket FX $.fx.off N • Selectors show() 1.3 • Attributes hide() toggle() • Traversing slideDown() • Manipulation slideUp() • CSS slideToggle() • Events fadeIn() fadeOut() • Effects fadeTo() • Ajax animate() • Utilities stop()
    • • Core $.fx.off • Selectors show() • Attributes hide() toggle() • Traversing slideDown() • Manipulation slideUp() • CSS slideToggle() • Events fadeIn() fadeOut() • Effects fadeTo() • Ajax animate() • Utilities stop()
    • • Core <!DOCTYPE html><html><head> <style> • Selectors div {background:#bca; width:100px; border:1px solid green;} • Attributes </style> </head> • Traversing <body> <div id="block">Hello!</div> • Manipulation <script src="jquery.js"></script> <script> • CSS $(‘#block’).animate({ • Events ! width: ‘70%’, ! opacity: 0.4, • Effects ! marginLeft: ‘0.6in’, ! fontSize: ‘3em’, • Ajax ! borderWidth: ‘10px’ }, 1500); http://jsbin.com/oroto/edit • Utilities </script></body></html>
    • • Core $.ajax() ajaxCompete() • Selectors $.get() $.post() ajaxError() ajaxSend() • Attributes $.getJSON() $.getScript() ajaxStart() ajaxStop() • Traversing $.ajaxSetup() ajaxSuccess() • Manipulation load() • CSS serialize() • Events serializeArray() • Effects • Ajax • Utilities
    • • Core $.ajax() ajaxCompete() • Selectors $.get() $.post() ajaxError() ajaxSend() • Attributes $.getJSON() $.getScript() ajaxStart() ajaxStop() • Traversing $.ajaxSetup() ajaxSuccess() • Manipulation load() • CSS serialize() • Events serializeArray() • Effects • Ajax • Utilities
    • • Core <!DOCTYPE html><html><body> • <script src="jquery.js"> Selectors </script> • Attributes <script> • Traversing $.getJSON("http://twitter.com/ statuses/user_timeline.json? • Manipulation screen_name=rem&callback=?", function(data){ • CSS $.each(data,function(i,tweet){ $('<p/>').html • Events (tweet.text).appendTo('body'); if ( i == 30 ) return false; • Effects }); • }); Ajax </script></body></html> • Utilities http://jsbin.com/acisi/edit
    • Plugins
    • 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()
    • Plugin design in 6 steps
    • Step 1: create a private scope for $ alias
    • <!DOCTYPE html><html><body> <script src=”jquery.js”></script> <script> (function ($) { })(jQuery); </script></body></html>
    • Step 2: attach plugin to fn alias (aka prototype)
    • <!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>
    • <!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>
    • Step 3: add implicit iteration
    • <!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>
    • Step 4: enable chaining
    • <!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>
    • <!DOCTYPE html><html><body> <p>I hate jQuery!</p> <p>I mean really hate jQuery!</p> <script src=”jquery.js”></script> <script> this == jQuery (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>
    • <!DOCTYPE html><html><body> <p>I hate jQuery!</p> <p>I mean really hate jQuery!</p> <script src=”jquery.js”></script> <script> this == jQuery (function ($) { $.fn.notHate = function () { return this.each(function () { $(this).text( $(this).text().replace(/hate/g, ‘love’) ); }); }; // end of == DOM element this plugin })(jQuery); $(‘p’).notHate().hide(); </script></body></html>
    • Step 5: add default options
    • <!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>
    • Step 6: add custom options
    • <!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>
    • <!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>
    • <!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>
    • <!DOCTYPE html><html><body> http://jsbin.com/ifuga/edit <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>
    • News • Four conferences next year: London, Boston, San Francisco and online • New plugin site • jQuery Forum (moving from Google Groups) • Mobile jQuery
    • Demo!
    • Remy Sharp @rem jQuery team member Co-author of O'Reilly jQuery Cookbook (due November 20th) jqueryfordesigners.com remysharp.com
    • Remy Sharp @rem jQuery team member Co-author of O'Reilly jQuery Cookbook (due November 20th) jqueryfordesigners.com ? Questions remysharp.com