SlideShare une entreprise Scribd logo
1  sur  50
Becoming a jQuery Expert
Purpose
Demonstrate what’s possible.
Full jQuery API: http://api.jquery.com/
Code
Download:
http://bit.ly/drdobbs-jq-zip
Browser Console
Chrome: Option-Command-J or Control-Shift-J
Firefox: Option-Command-K or Control-Shift-K
Internet Explorer: F12
Safari:
• Advanced Settings
• Check box next to “Show Develop menu in menu bar”
•Option-Command-C
DOM Traversal
DOM Traversal
$('p');
[<p></p>]
DOM Traversal
Filtering
$('p').first();
[<p id="s1p1" class="active section1">Paragraph 1</p>]​ ​ ​ ​
$('p').last();
[<p id="s2p4" class="active section2">Paragraph 4</p>]​ ​ ​ ​
$('ul').find('li');
[<li data-price="100">Unordered list item 1</li>,​ ​ ​
<li data-price="275">Unordered list item 2</li>,​ ​ ​
<li data-price="50">Unordered list item 3</li>,​ ​ ​
<li data-price="150">Unordered list item 4</li>]​ ​ ​
DOM Traversal
Filtering
$('p').filter('.inactive');
[<p id="s1p2" class="inactive section1">Paragraph 2</p>,​ ​ ​ ​ ​
<p id="s2p2" class="inactive section2">Paragraph 2</p>]​ ​ ​ ​
$('p').filter(function () {
return $(this).hasClass('inactive');
});
$('p').map(function () {
return this.id;
});
["s1p1", "s1p2", "s1p3", "s1p4", "s2p1", "s2p2", "s2p3", "s2p4"]
DOM Traversal
A deeper look at .map
$('p').map(function (idx, el) {
var text = $(el).text(),
num = text.replace(/Paragraphs/, ''),
num = parseInt(num, 10);
return num % 2 === 0 ? el : undefined;
});
[<p id="s1p2" class="inactive section1">Paragraph 2</p>,​ ​ ​ ​
<p id="s1p4" class="active section1">Paragraph 4</p>,​ ​ ​ ​
<p id="s2p2" class="inactive section2">Paragraph 2</p>,​ ​ ​ ​
<p id="s2p4" class="active section2">Paragraph 4</p>]​ ​ ​ ​
Try it!Try it!
DOM Traversal
Getting elements relative to a start point
$('#s2').prev();
[<section id="s1">…</section>]​ ​ ​
$('#s2p4').prevAll();
[<p id="s2p3" class="active section2">Paragraph 3</p>,​ ​ ​ ​
<p id="s2p2" class="inactive section2">Paragraph 2</p>,​ ​ ​ ​
<p id="s2p1" class="active section2">Paragraph 1</p>,​ ​ ​ ​
<h1>Section 2</h1>]​ ​
$('#s2p4').prevUntil('.inactive');
[<p id="s2p3" class="active section2">Paragraph 3</p>]​ ​ ​ ​
DOM Traversal
Getting elements relative to a start point
$('#s1').next();
[<section id="s2">…</section>]​ ​ ​
$('#s1p1').nextAll();
[<p id="s1p2" class="inactive section1">Paragraph 2</p>,​ ​ ​ ​
<p id="s1p3" class="active section1">Paragraph 3</p>,​ ​ ​
<p id="s1p4" class="active section1">Paragraph 4</p>,​ ​ ​
<ul>…</ul>]​ ​
$('#s1p1').nextUntil('ul');
<p id="s1p2" class="inactive section1">Paragraph 2</p>,​ ​ ​ ​
<p id="s1p3" class="active section1">Paragraph 3</p>,​ ​ ​ ​
<p id="s1p4" class="active section1">Paragraph 4</p>]​ ​ ​ ​
DOM Traversal
Getting elements relative to a start point
$('#s1').parent();
[<div id="markup">…</div>]​ ​
$('p').closest('section');
[<section id="s1">…</section>,​ ​ ​
<section id="s2">…</section>]​ ​ ​
$('#s1p3').siblings();
[<h1>Section 1</h1>,​ ​
<p id="s1p1" class="active section1">Paragraph 1</p>,​ ​ ​
<p id="s1p2" class="inactive section1">Paragraph 2</p>,​ ​ ​ ​
<p id="s1p4" class="active section1">Paragraph 4</p>,​ ​ ​
<ul>…</ul>]​ ​
DOM Traversal
Chaining traversal methods
$('#s1p3').siblings().andSelf();
[<h1>Section 1</h1>,​ ​
<p id="s1p1" class="active section1">Paragraph 1</p>,​
<p id="s1p2" class="inactive section1">Paragraph 2</p>,​ ​
<p id="s1p3" class="inactive section1">Paragraph 3</p>,​ ​
<p id="s1p4" class="active section1">Paragraph 4</p>,​
<ul>…</ul>]​ ​
$('#s1p4').parent().next();
[<section id="s2">…</section>]​ ​ ​
$('#s2p1').add($('#s1p1'));
[<p id="s1p1" class="active section1">Paragraph 1</p>,​ ​ ​ ​
<p id="s2p1" class="active section2">Paragraph 1</p>]​ ​ ​ ​
Try it!Try it!
DOM Traversal
Some filters can be expressed as selectors.*
*May impact performance.
DOM Traversal
Filters as selectors
$('p:even');
[<p id="s1p1" class="active section1">Paragraph 1</p>,​ ​ ​ ​
<p id="s1p3" class="active section1">Paragraph 3</p>,​ ​ ​ ​
<p id="s2p1" class="active section2">Paragraph 1</p>,​ ​ ​ ​
<p id="s2p3" class="active section2">Paragraph 3</p>] // zero-based​ ​ ​ ​
$('p:odd');
$('p:first');
$('p:last');
$('p:not(".inactive")');
DOM Traversal
More selectors
$(':header');
[<h1>Section 1</h1>,​ ​
<h1>Section 2</h1>]​ ​
$('li:nth-child(2)');
[<li data-price="275">Unordered list item 2</li>,​ ​ ​
<li>Ordered list item 2</li>]​ ​
DOM Traversal
Adding context to narrow things down
$('li');
[<li data-price="100">Unordered list item 1</li>,​ ​ ​
<li data-price="275">Unordered list item 2</li>,​ ​ ​
<li data-price="50">Unordered list item 3</li>,​ ​ ​
<li data-price="150">Unordered list item 4</li>,​ ​ ​
<li>Ordered list item 1</li>,​ ​
<li>Ordered list item 2</li>,​ ​
<li>Ordered list item 3</li>,​ ​
<li>Ordered list item 4</li>]​ ​
$('li', 'ul');
[<li data-price="100">Unordered list item 1</li>,​ ​ ​
<li data-price="275">Unordered list item 2</li>,​ ​ ​
<li data-price="50">Unordered list item 3</li>,​ ​ ​
<li data-price="150">Unordered list item 4</li>]​ ​ ​
DOM Traversal
Adding context to narrow things down
$(':nth-child(2)');
[<style>…</style>,​ ​
<body>…</body>,​ ​
<p id="s1p1" class="active section1">Paragraph 1</p>,​ ​ ​ ​
<li data-price="275">Unordered list item 2</li>,​ ​ ​
<section id="s2">…</section>,​ ​ ​
<p id="s2p1" class="active section2">Paragraph 1</p>,​ ​ ​ ​
<li>Ordered list item 2</li>]​ ​
$(':nth-child(2)', 'ul');
[<li data-price="275">Unordered list item 2</li>]​ ​ ​
Try it!Try it!
DOM Traversal
Reading the inner HTML of an element
$('ol').html();
"
<li>Ordered list item 1</li>
<li>Ordered list item 2</li>
<li>Ordered list item 3</li>
<li>Ordered list item 4</li>
"
DOM Traversal
Reading the inner text of an element
$('ol').text();
"
Ordered list item 1
Ordered list item 2
Ordered list item 3
Ordered list item 4
"
DOM Manipulation
DOM Manipulation
Element Creation
$('<p></p>');
[<p></p>]
Fragment. Detached from DOM.
DOM Manipulation
Element Creation
$('<p>Where’s the party?</p>').appendTo('#s1');
$('#s1').append('<p>I’m new!</p>');
$('#s1').prepend('<p>So am I!</p>');
Try it!Try it!
DOM Manipulation
Element Creation
$('li').wrap('<div></div>');
<li>…</li>
<li>…</li>
…
<div>
<li>…</li>
</div>
<div>
<li>…</li>
</div>
…
$('li').unwrap();
DOM Manipulation
Element Creation
$('li').wrapInner('<div></div>');
<li>…</li>
<li>
<div>…</div>
</li>
$('li').wrapAll('<div></div>');
<li>…</li>
<li>…</li>
…
<div>
<li>…</li>
<li>…</li>
</div>
DOM Manipulation
Attribute Creation
$('<p class="foo" id="bar" style="color:
red;">hello</p>');
[<p class="foo" id="bar" style="color: red;">hello</p>]​ ​ ​ ​ ​ ​ ​
DOM Manipulation
Attribute Creation
$('p')
.addClass('foo')
.attr('id', 'bar')
.css('color', 'red');
[<p class="foo" id="bar" style="color: red;">hello</p>]​ ​ ​ ​ ​ ​ ​
Try it!Try it!
Data Storage
Data Storage
Read data-* attributes from the DOM
<div id="appointment1" data-date="2014-04-11">April 11, 2014</div>
$('#appointment1').data('date');
"2014-04-11"
Data Storage
Read data-* attributes from the DOM
<ul>
<li data-price="100">Unordered list item 1</li>
<li data-price="275">Unordered list item 2</li>
<li data-price="50">Unordered list item 3</li>
<li data-price="150">Unordered list item 4</li>
</ul>
$('ul li').data('price');
100
Data Storage
Read data-* attributes from the DOM
<ul>
<li data-price="100">Unordered list item 1</li>
<li data-price="275">Unordered list item 2</li>
<li data-price="50">Unordered list item 3</li>
<li data-price="150">Unordered list item 4</li>
</ul>
$('ul li').each(function (idx, el) {
console.log($(el).data('price'));
});
Try it!Try it!
Data Storage
Write data-* attributes to elements*
<div id="appointment2">April 11, 2014</div>
$('#appointment1').data('date', '2014-04-11');
*Cached internally. Not written to the DOM.
Data Storage
Write data-* attributes to elements*
$('ol li').data('price', 100);
*Cached internally. Not written to the DOM.
Your turn!
Data Storage
Data Storage
1) Read the price data from the unordered list items
2) Add a 10% tax to each value
3) Write the newly taxed price to each ordered list counterpart
$('ul li').each(function (idx, el) {
var price = $(el).data('price') * 1.10;
$('ol li').eq(idx).data('price', price);
});
Events
Events
Direct event handler assignment
$('a').on('click', function (e) {
e.preventDefault();
// do something
});
Events
Direct event handler assignment with data
$('a').on('click',
{priority: 'high'},
function (e) {
e.preventDefault();
console.log(e.data.priority);
});
Events
Delegated event handler assignment
$(document).on('click', 'a', function (e) {
e.preventDefault();
// do something
});
Faster.
Future-friendly.
Events
Custom event
$('.state').on('dataReady', function (e, payload) {
$(this).addClass(payload.state);
});
$('.state').trigger('dataReady', {
state: 'pending'
});
Events
Namespaced event
$('a').on('click.loggedIn', function (e) {
console.log('logged in click')
});
$('a').on('mouseover.loggedIn', function (e) {
console.log('logged in mouseover');
});
$('a').off('.loggedIn');
Events
Stopping propagation
$('li').on('click', function (e) {
console.log('li clicked!');
});
$('ul').on('click', function (e) {
console.log('ul clicked!');
});
Events
Stopping propagation
$('li').on('click', function (e) {
e.stopPropagation();
console.log('li clicked!');
});
$('ul').on('click', function (e) {
console.log('ul clicked!');
});
Try it!Try it!
Events
Using the right ready
$(document).on('ready', function () {
console.log('Will not fire if assigned late.');
});
$(document).ready(function () {
console.log('Will fire even if assigned late.');
});
Custom Selectors
Custom Selectors
We saw:
$('p:first');
Which is actually just:
$.expr[':'].first = function (el) {
…
}
Custom Selectors
We can write whatever we want:
$.expr[':'].inactive = function (el) {
return $(el).hasClass('inactive');
};
Which we can then use:
$('p:inactive');
[<p id="s1p2" class="inactive section1">Paragraph 2</p>,​ ​ ​ ​
<p id="s2p2" class="inactive section2">Paragraph 2</p>]​ ​ ​ ​
Try it!Try it!
Building a Plugin
Building a Plugin
$.fn.hideRemove = function () {
return this.hide('slow', function () {
$(this).remove();
});
};
Which we can then use:
$('p').hideRemove();
Try it!Try it!
Thank you!
Happy to answer your questions throughout
the conference and afterwards!
ara.pehlivanian@gmail.com
twitter.com/ara_p

Contenu connexe

Tendances

Working With JQuery Part1
Working With JQuery Part1Working With JQuery Part1
Working With JQuery Part1saydin_soft
 
Symfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technologySymfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technologyDaniel Knell
 
20220112 sac v1
20220112 sac v120220112 sac v1
20220112 sac v1Sharon Liu
 
Silex meets SOAP & REST
Silex meets SOAP & RESTSilex meets SOAP & REST
Silex meets SOAP & RESTHugo Hamon
 
Refactoring using Codeception
Refactoring using CodeceptionRefactoring using Codeception
Refactoring using CodeceptionJeroen van Dijk
 
Xlab #1: Advantages of functional programming in Java 8
Xlab #1: Advantages of functional programming in Java 8Xlab #1: Advantages of functional programming in Java 8
Xlab #1: Advantages of functional programming in Java 8XSolve
 
Hidden in plain site – joomla! hidden secrets for code monkeys
Hidden in plain site – joomla! hidden secrets for code monkeysHidden in plain site – joomla! hidden secrets for code monkeys
Hidden in plain site – joomla! hidden secrets for code monkeysNicholas Dionysopoulos
 
An Elephant of a Different Colour: Hack
An Elephant of a Different Colour: HackAn Elephant of a Different Colour: Hack
An Elephant of a Different Colour: HackVic Metcalfe
 
Templates don’t need to break the browser by Nikolas Martens
Templates don’t need to break the browser by Nikolas Martens  Templates don’t need to break the browser by Nikolas Martens
Templates don’t need to break the browser by Nikolas Martens Codemotion
 
Templating you're doing it wrong - Nikolas Martens - Codemotion Amsterdam 2017
Templating you're doing it wrong - Nikolas Martens - Codemotion Amsterdam 2017Templating you're doing it wrong - Nikolas Martens - Codemotion Amsterdam 2017
Templating you're doing it wrong - Nikolas Martens - Codemotion Amsterdam 2017Codemotion
 
循環参照のはなし
循環参照のはなし循環参照のはなし
循環参照のはなしMasahiro Honma
 
PHP Language Trivia
PHP Language TriviaPHP Language Trivia
PHP Language TriviaNikita Popov
 

Tendances (20)

Working With JQuery Part1
Working With JQuery Part1Working With JQuery Part1
Working With JQuery Part1
 
Php My Sql
Php My SqlPhp My Sql
Php My Sql
 
JQuery
JQueryJQuery
JQuery
 
Symfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technologySymfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technology
 
20220112 sac v1
20220112 sac v120220112 sac v1
20220112 sac v1
 
Inc
IncInc
Inc
 
Silex meets SOAP & REST
Silex meets SOAP & RESTSilex meets SOAP & REST
Silex meets SOAP & REST
 
Refactoring using Codeception
Refactoring using CodeceptionRefactoring using Codeception
Refactoring using Codeception
 
Xlab #1: Advantages of functional programming in Java 8
Xlab #1: Advantages of functional programming in Java 8Xlab #1: Advantages of functional programming in Java 8
Xlab #1: Advantages of functional programming in Java 8
 
Backbone - TDC 2011 Floripa
Backbone - TDC 2011 FloripaBackbone - TDC 2011 Floripa
Backbone - TDC 2011 Floripa
 
Perl basics for Pentesters
Perl basics for PentestersPerl basics for Pentesters
Perl basics for Pentesters
 
Hidden in plain site – joomla! hidden secrets for code monkeys
Hidden in plain site – joomla! hidden secrets for code monkeysHidden in plain site – joomla! hidden secrets for code monkeys
Hidden in plain site – joomla! hidden secrets for code monkeys
 
An Elephant of a Different Colour: Hack
An Elephant of a Different Colour: HackAn Elephant of a Different Colour: Hack
An Elephant of a Different Colour: Hack
 
Sorting arrays in PHP
Sorting arrays in PHPSorting arrays in PHP
Sorting arrays in PHP
 
Php Sq Lite
Php Sq LitePhp Sq Lite
Php Sq Lite
 
Templates don’t need to break the browser by Nikolas Martens
Templates don’t need to break the browser by Nikolas Martens  Templates don’t need to break the browser by Nikolas Martens
Templates don’t need to break the browser by Nikolas Martens
 
Templating you're doing it wrong - Nikolas Martens - Codemotion Amsterdam 2017
Templating you're doing it wrong - Nikolas Martens - Codemotion Amsterdam 2017Templating you're doing it wrong - Nikolas Martens - Codemotion Amsterdam 2017
Templating you're doing it wrong - Nikolas Martens - Codemotion Amsterdam 2017
 
Views notwithstanding
Views notwithstandingViews notwithstanding
Views notwithstanding
 
循環参照のはなし
循環参照のはなし循環参照のはなし
循環参照のはなし
 
PHP Language Trivia
PHP Language TriviaPHP Language Trivia
PHP Language Trivia
 

Similaire à Becoming a jQuery expert

Event handling using jQuery
Event handling using jQueryEvent handling using jQuery
Event handling using jQueryIban Martinez
 
Practical PHP by example Jan Leth-Kjaer
Practical PHP by example   Jan Leth-KjaerPractical PHP by example   Jan Leth-Kjaer
Practical PHP by example Jan Leth-KjaerCOMMON Europe
 
jQuery: Tips, tricks and hints for better development and Performance
jQuery: Tips, tricks and hints for better development and PerformancejQuery: Tips, tricks and hints for better development and Performance
jQuery: Tips, tricks and hints for better development and PerformanceJonas De Smet
 
Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011
Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011
Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011Masahiro Nagano
 
international PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secretsinternational PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secretssmueller_sandsmedia
 
Undercover Pods / WP Functions
Undercover Pods / WP FunctionsUndercover Pods / WP Functions
Undercover Pods / WP Functionspodsframework
 
Advanced jQuery
Advanced jQueryAdvanced jQuery
Advanced jQuerysergioafp
 
Meet Magento Sweden - Magento 2 Layout and Code Compilation for Performance
Meet Magento Sweden - Magento 2 Layout and Code Compilation for PerformanceMeet Magento Sweden - Magento 2 Layout and Code Compilation for Performance
Meet Magento Sweden - Magento 2 Layout and Code Compilation for PerformanceIvan Chepurnyi
 
web design and jQuery introduction in persian
web design and jQuery introduction in persianweb design and jQuery introduction in persian
web design and jQuery introduction in persianAhmad Badpey
 
Taming that client side mess with Backbone.js
Taming that client side mess with Backbone.jsTaming that client side mess with Backbone.js
Taming that client side mess with Backbone.jsJarod Ferguson
 
R57shell
R57shellR57shell
R57shellady36
 

Similaire à Becoming a jQuery expert (20)

Migrare da symfony 1 a Symfony2
 Migrare da symfony 1 a Symfony2  Migrare da symfony 1 a Symfony2
Migrare da symfony 1 a Symfony2
 
Event handling using jQuery
Event handling using jQueryEvent handling using jQuery
Event handling using jQuery
 
logic321
logic321logic321
logic321
 
Practical PHP by example Jan Leth-Kjaer
Practical PHP by example   Jan Leth-KjaerPractical PHP by example   Jan Leth-Kjaer
Practical PHP by example Jan Leth-Kjaer
 
JQuery
JQueryJQuery
JQuery
 
Jquery mobile2
Jquery mobile2Jquery mobile2
Jquery mobile2
 
jQuery: Tips, tricks and hints for better development and Performance
jQuery: Tips, tricks and hints for better development and PerformancejQuery: Tips, tricks and hints for better development and Performance
jQuery: Tips, tricks and hints for better development and Performance
 
Emmet cheat-sheet
Emmet cheat-sheetEmmet cheat-sheet
Emmet cheat-sheet
 
Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011
Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011
Designing Opeation Oriented Web Applications / YAPC::Asia Tokyo 2011
 
J querypractice
J querypracticeJ querypractice
J querypractice
 
international PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secretsinternational PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secrets
 
course js day 3
course js day 3course js day 3
course js day 3
 
Undercover Pods / WP Functions
Undercover Pods / WP FunctionsUndercover Pods / WP Functions
Undercover Pods / WP Functions
 
Advanced jQuery
Advanced jQueryAdvanced jQuery
Advanced jQuery
 
Meet Magento Sweden - Magento 2 Layout and Code Compilation for Performance
Meet Magento Sweden - Magento 2 Layout and Code Compilation for PerformanceMeet Magento Sweden - Magento 2 Layout and Code Compilation for Performance
Meet Magento Sweden - Magento 2 Layout and Code Compilation for Performance
 
Jquery 3
Jquery 3Jquery 3
Jquery 3
 
jQuery quick tuts
jQuery quick tutsjQuery quick tuts
jQuery quick tuts
 
web design and jQuery introduction in persian
web design and jQuery introduction in persianweb design and jQuery introduction in persian
web design and jQuery introduction in persian
 
Taming that client side mess with Backbone.js
Taming that client side mess with Backbone.jsTaming that client side mess with Backbone.js
Taming that client side mess with Backbone.js
 
R57shell
R57shellR57shell
R57shell
 

Plus de Ara Pehlivanian

Is it CrossFit or JavaScript?
Is it CrossFit or JavaScript?Is it CrossFit or JavaScript?
Is it CrossFit or JavaScript?Ara Pehlivanian
 
Expert JavaScript tricks of the masters
Expert JavaScript  tricks of the mastersExpert JavaScript  tricks of the masters
Expert JavaScript tricks of the mastersAra Pehlivanian
 
YUI 3: The Most Advance JavaScript Library in the World
YUI 3: The Most Advance JavaScript Library in the WorldYUI 3: The Most Advance JavaScript Library in the World
YUI 3: The Most Advance JavaScript Library in the WorldAra Pehlivanian
 
Twitterface: A viral marketing concept
Twitterface: A viral marketing conceptTwitterface: A viral marketing concept
Twitterface: A viral marketing conceptAra Pehlivanian
 
Worry Free Web Development
Worry Free Web DevelopmentWorry Free Web Development
Worry Free Web DevelopmentAra Pehlivanian
 

Plus de Ara Pehlivanian (8)

Is it CrossFit or JavaScript?
Is it CrossFit or JavaScript?Is it CrossFit or JavaScript?
Is it CrossFit or JavaScript?
 
Expert JavaScript tricks of the masters
Expert JavaScript  tricks of the mastersExpert JavaScript  tricks of the masters
Expert JavaScript tricks of the masters
 
YUI 3: The Most Advance JavaScript Library in the World
YUI 3: The Most Advance JavaScript Library in the WorldYUI 3: The Most Advance JavaScript Library in the World
YUI 3: The Most Advance JavaScript Library in the World
 
YUI Gallery
YUI GalleryYUI Gallery
YUI Gallery
 
Master your domain
Master your domainMaster your domain
Master your domain
 
Twitterface: A viral marketing concept
Twitterface: A viral marketing conceptTwitterface: A viral marketing concept
Twitterface: A viral marketing concept
 
Worry Free Web Development
Worry Free Web DevelopmentWorry Free Web Development
Worry Free Web Development
 
Scripting The Dom
Scripting The DomScripting The Dom
Scripting The Dom
 

Dernier

Call Girls In The Ocean Pearl Retreat Hotel New Delhi 9873777170
Call Girls In The Ocean Pearl Retreat Hotel New Delhi 9873777170Call Girls In The Ocean Pearl Retreat Hotel New Delhi 9873777170
Call Girls In The Ocean Pearl Retreat Hotel New Delhi 9873777170Sonam Pathan
 
Potsdam FH学位证,波茨坦应用技术大学毕业证书1:1制作
Potsdam FH学位证,波茨坦应用技术大学毕业证书1:1制作Potsdam FH学位证,波茨坦应用技术大学毕业证书1:1制作
Potsdam FH学位证,波茨坦应用技术大学毕业证书1:1制作ys8omjxb
 
Font Performance - NYC WebPerf Meetup April '24
Font Performance - NYC WebPerf Meetup April '24Font Performance - NYC WebPerf Meetup April '24
Font Performance - NYC WebPerf Meetup April '24Paul Calvano
 
Q4-1-Illustrating-Hypothesis-Testing.pptx
Q4-1-Illustrating-Hypothesis-Testing.pptxQ4-1-Illustrating-Hypothesis-Testing.pptx
Q4-1-Illustrating-Hypothesis-Testing.pptxeditsforyah
 
办理(UofR毕业证书)罗切斯特大学毕业证成绩单原版一比一
办理(UofR毕业证书)罗切斯特大学毕业证成绩单原版一比一办理(UofR毕业证书)罗切斯特大学毕业证成绩单原版一比一
办理(UofR毕业证书)罗切斯特大学毕业证成绩单原版一比一z xss
 
『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书
『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书
『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书rnrncn29
 
Film cover research (1).pptxsdasdasdasdasdasa
Film cover research (1).pptxsdasdasdasdasdasaFilm cover research (1).pptxsdasdasdasdasdasa
Film cover research (1).pptxsdasdasdasdasdasa494f574xmv
 
办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书
办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书
办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书zdzoqco
 
Contact Rya Baby for Call Girls New Delhi
Contact Rya Baby for Call Girls New DelhiContact Rya Baby for Call Girls New Delhi
Contact Rya Baby for Call Girls New Delhimiss dipika
 
Top 10 Interactive Website Design Trends in 2024.pptx
Top 10 Interactive Website Design Trends in 2024.pptxTop 10 Interactive Website Design Trends in 2024.pptx
Top 10 Interactive Website Design Trends in 2024.pptxDyna Gilbert
 
Call Girls Near The Suryaa Hotel New Delhi 9873777170
Call Girls Near The Suryaa Hotel New Delhi 9873777170Call Girls Near The Suryaa Hotel New Delhi 9873777170
Call Girls Near The Suryaa Hotel New Delhi 9873777170Sonam Pathan
 
PHP-based rendering of TYPO3 Documentation
PHP-based rendering of TYPO3 DocumentationPHP-based rendering of TYPO3 Documentation
PHP-based rendering of TYPO3 DocumentationLinaWolf1
 
SCM Symposium PPT Format Customer loyalty is predi
SCM Symposium PPT Format Customer loyalty is prediSCM Symposium PPT Format Customer loyalty is predi
SCM Symposium PPT Format Customer loyalty is predieusebiomeyer
 
『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书
『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书
『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书rnrncn29
 
NSX-T and Service Interfaces presentation
NSX-T and Service Interfaces presentationNSX-T and Service Interfaces presentation
NSX-T and Service Interfaces presentationMarko4394
 

Dernier (17)

Call Girls In The Ocean Pearl Retreat Hotel New Delhi 9873777170
Call Girls In The Ocean Pearl Retreat Hotel New Delhi 9873777170Call Girls In The Ocean Pearl Retreat Hotel New Delhi 9873777170
Call Girls In The Ocean Pearl Retreat Hotel New Delhi 9873777170
 
young call girls in Uttam Nagar🔝 9953056974 🔝 Delhi escort Service
young call girls in Uttam Nagar🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Uttam Nagar🔝 9953056974 🔝 Delhi escort Service
young call girls in Uttam Nagar🔝 9953056974 🔝 Delhi escort Service
 
Potsdam FH学位证,波茨坦应用技术大学毕业证书1:1制作
Potsdam FH学位证,波茨坦应用技术大学毕业证书1:1制作Potsdam FH学位证,波茨坦应用技术大学毕业证书1:1制作
Potsdam FH学位证,波茨坦应用技术大学毕业证书1:1制作
 
Font Performance - NYC WebPerf Meetup April '24
Font Performance - NYC WebPerf Meetup April '24Font Performance - NYC WebPerf Meetup April '24
Font Performance - NYC WebPerf Meetup April '24
 
Q4-1-Illustrating-Hypothesis-Testing.pptx
Q4-1-Illustrating-Hypothesis-Testing.pptxQ4-1-Illustrating-Hypothesis-Testing.pptx
Q4-1-Illustrating-Hypothesis-Testing.pptx
 
办理(UofR毕业证书)罗切斯特大学毕业证成绩单原版一比一
办理(UofR毕业证书)罗切斯特大学毕业证成绩单原版一比一办理(UofR毕业证书)罗切斯特大学毕业证成绩单原版一比一
办理(UofR毕业证书)罗切斯特大学毕业证成绩单原版一比一
 
Hot Sexy call girls in Rk Puram 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in  Rk Puram 🔝 9953056974 🔝 Delhi escort ServiceHot Sexy call girls in  Rk Puram 🔝 9953056974 🔝 Delhi escort Service
Hot Sexy call girls in Rk Puram 🔝 9953056974 🔝 Delhi escort Service
 
『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书
『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书
『澳洲文凭』买詹姆士库克大学毕业证书成绩单办理澳洲JCU文凭学位证书
 
Film cover research (1).pptxsdasdasdasdasdasa
Film cover research (1).pptxsdasdasdasdasdasaFilm cover research (1).pptxsdasdasdasdasdasa
Film cover research (1).pptxsdasdasdasdasdasa
 
办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书
办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书
办理多伦多大学毕业证成绩单|购买加拿大UTSG文凭证书
 
Contact Rya Baby for Call Girls New Delhi
Contact Rya Baby for Call Girls New DelhiContact Rya Baby for Call Girls New Delhi
Contact Rya Baby for Call Girls New Delhi
 
Top 10 Interactive Website Design Trends in 2024.pptx
Top 10 Interactive Website Design Trends in 2024.pptxTop 10 Interactive Website Design Trends in 2024.pptx
Top 10 Interactive Website Design Trends in 2024.pptx
 
Call Girls Near The Suryaa Hotel New Delhi 9873777170
Call Girls Near The Suryaa Hotel New Delhi 9873777170Call Girls Near The Suryaa Hotel New Delhi 9873777170
Call Girls Near The Suryaa Hotel New Delhi 9873777170
 
PHP-based rendering of TYPO3 Documentation
PHP-based rendering of TYPO3 DocumentationPHP-based rendering of TYPO3 Documentation
PHP-based rendering of TYPO3 Documentation
 
SCM Symposium PPT Format Customer loyalty is predi
SCM Symposium PPT Format Customer loyalty is prediSCM Symposium PPT Format Customer loyalty is predi
SCM Symposium PPT Format Customer loyalty is predi
 
『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书
『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书
『澳洲文凭』买拉筹伯大学毕业证书成绩单办理澳洲LTU文凭学位证书
 
NSX-T and Service Interfaces presentation
NSX-T and Service Interfaces presentationNSX-T and Service Interfaces presentation
NSX-T and Service Interfaces presentation
 

Becoming a jQuery expert

  • 2. Purpose Demonstrate what’s possible. Full jQuery API: http://api.jquery.com/
  • 4. Browser Console Chrome: Option-Command-J or Control-Shift-J Firefox: Option-Command-K or Control-Shift-K Internet Explorer: F12 Safari: • Advanced Settings • Check box next to “Show Develop menu in menu bar” •Option-Command-C
  • 7. DOM Traversal Filtering $('p').first(); [<p id="s1p1" class="active section1">Paragraph 1</p>]​ ​ ​ ​ $('p').last(); [<p id="s2p4" class="active section2">Paragraph 4</p>]​ ​ ​ ​ $('ul').find('li'); [<li data-price="100">Unordered list item 1</li>,​ ​ ​ <li data-price="275">Unordered list item 2</li>,​ ​ ​ <li data-price="50">Unordered list item 3</li>,​ ​ ​ <li data-price="150">Unordered list item 4</li>]​ ​ ​
  • 8. DOM Traversal Filtering $('p').filter('.inactive'); [<p id="s1p2" class="inactive section1">Paragraph 2</p>,​ ​ ​ ​ ​ <p id="s2p2" class="inactive section2">Paragraph 2</p>]​ ​ ​ ​ $('p').filter(function () { return $(this).hasClass('inactive'); }); $('p').map(function () { return this.id; }); ["s1p1", "s1p2", "s1p3", "s1p4", "s2p1", "s2p2", "s2p3", "s2p4"]
  • 9. DOM Traversal A deeper look at .map $('p').map(function (idx, el) { var text = $(el).text(), num = text.replace(/Paragraphs/, ''), num = parseInt(num, 10); return num % 2 === 0 ? el : undefined; }); [<p id="s1p2" class="inactive section1">Paragraph 2</p>,​ ​ ​ ​ <p id="s1p4" class="active section1">Paragraph 4</p>,​ ​ ​ ​ <p id="s2p2" class="inactive section2">Paragraph 2</p>,​ ​ ​ ​ <p id="s2p4" class="active section2">Paragraph 4</p>]​ ​ ​ ​ Try it!Try it!
  • 10. DOM Traversal Getting elements relative to a start point $('#s2').prev(); [<section id="s1">…</section>]​ ​ ​ $('#s2p4').prevAll(); [<p id="s2p3" class="active section2">Paragraph 3</p>,​ ​ ​ ​ <p id="s2p2" class="inactive section2">Paragraph 2</p>,​ ​ ​ ​ <p id="s2p1" class="active section2">Paragraph 1</p>,​ ​ ​ ​ <h1>Section 2</h1>]​ ​ $('#s2p4').prevUntil('.inactive'); [<p id="s2p3" class="active section2">Paragraph 3</p>]​ ​ ​ ​
  • 11. DOM Traversal Getting elements relative to a start point $('#s1').next(); [<section id="s2">…</section>]​ ​ ​ $('#s1p1').nextAll(); [<p id="s1p2" class="inactive section1">Paragraph 2</p>,​ ​ ​ ​ <p id="s1p3" class="active section1">Paragraph 3</p>,​ ​ ​ <p id="s1p4" class="active section1">Paragraph 4</p>,​ ​ ​ <ul>…</ul>]​ ​ $('#s1p1').nextUntil('ul'); <p id="s1p2" class="inactive section1">Paragraph 2</p>,​ ​ ​ ​ <p id="s1p3" class="active section1">Paragraph 3</p>,​ ​ ​ ​ <p id="s1p4" class="active section1">Paragraph 4</p>]​ ​ ​ ​
  • 12. DOM Traversal Getting elements relative to a start point $('#s1').parent(); [<div id="markup">…</div>]​ ​ $('p').closest('section'); [<section id="s1">…</section>,​ ​ ​ <section id="s2">…</section>]​ ​ ​ $('#s1p3').siblings(); [<h1>Section 1</h1>,​ ​ <p id="s1p1" class="active section1">Paragraph 1</p>,​ ​ ​ <p id="s1p2" class="inactive section1">Paragraph 2</p>,​ ​ ​ ​ <p id="s1p4" class="active section1">Paragraph 4</p>,​ ​ ​ <ul>…</ul>]​ ​
  • 13. DOM Traversal Chaining traversal methods $('#s1p3').siblings().andSelf(); [<h1>Section 1</h1>,​ ​ <p id="s1p1" class="active section1">Paragraph 1</p>,​ <p id="s1p2" class="inactive section1">Paragraph 2</p>,​ ​ <p id="s1p3" class="inactive section1">Paragraph 3</p>,​ ​ <p id="s1p4" class="active section1">Paragraph 4</p>,​ <ul>…</ul>]​ ​ $('#s1p4').parent().next(); [<section id="s2">…</section>]​ ​ ​ $('#s2p1').add($('#s1p1')); [<p id="s1p1" class="active section1">Paragraph 1</p>,​ ​ ​ ​ <p id="s2p1" class="active section2">Paragraph 1</p>]​ ​ ​ ​ Try it!Try it!
  • 14. DOM Traversal Some filters can be expressed as selectors.* *May impact performance.
  • 15. DOM Traversal Filters as selectors $('p:even'); [<p id="s1p1" class="active section1">Paragraph 1</p>,​ ​ ​ ​ <p id="s1p3" class="active section1">Paragraph 3</p>,​ ​ ​ ​ <p id="s2p1" class="active section2">Paragraph 1</p>,​ ​ ​ ​ <p id="s2p3" class="active section2">Paragraph 3</p>] // zero-based​ ​ ​ ​ $('p:odd'); $('p:first'); $('p:last'); $('p:not(".inactive")');
  • 16. DOM Traversal More selectors $(':header'); [<h1>Section 1</h1>,​ ​ <h1>Section 2</h1>]​ ​ $('li:nth-child(2)'); [<li data-price="275">Unordered list item 2</li>,​ ​ ​ <li>Ordered list item 2</li>]​ ​
  • 17. DOM Traversal Adding context to narrow things down $('li'); [<li data-price="100">Unordered list item 1</li>,​ ​ ​ <li data-price="275">Unordered list item 2</li>,​ ​ ​ <li data-price="50">Unordered list item 3</li>,​ ​ ​ <li data-price="150">Unordered list item 4</li>,​ ​ ​ <li>Ordered list item 1</li>,​ ​ <li>Ordered list item 2</li>,​ ​ <li>Ordered list item 3</li>,​ ​ <li>Ordered list item 4</li>]​ ​ $('li', 'ul'); [<li data-price="100">Unordered list item 1</li>,​ ​ ​ <li data-price="275">Unordered list item 2</li>,​ ​ ​ <li data-price="50">Unordered list item 3</li>,​ ​ ​ <li data-price="150">Unordered list item 4</li>]​ ​ ​
  • 18. DOM Traversal Adding context to narrow things down $(':nth-child(2)'); [<style>…</style>,​ ​ <body>…</body>,​ ​ <p id="s1p1" class="active section1">Paragraph 1</p>,​ ​ ​ ​ <li data-price="275">Unordered list item 2</li>,​ ​ ​ <section id="s2">…</section>,​ ​ ​ <p id="s2p1" class="active section2">Paragraph 1</p>,​ ​ ​ ​ <li>Ordered list item 2</li>]​ ​ $(':nth-child(2)', 'ul'); [<li data-price="275">Unordered list item 2</li>]​ ​ ​ Try it!Try it!
  • 19. DOM Traversal Reading the inner HTML of an element $('ol').html(); " <li>Ordered list item 1</li> <li>Ordered list item 2</li> <li>Ordered list item 3</li> <li>Ordered list item 4</li> "
  • 20. DOM Traversal Reading the inner text of an element $('ol').text(); " Ordered list item 1 Ordered list item 2 Ordered list item 3 Ordered list item 4 "
  • 23. DOM Manipulation Element Creation $('<p>Where’s the party?</p>').appendTo('#s1'); $('#s1').append('<p>I’m new!</p>'); $('#s1').prepend('<p>So am I!</p>'); Try it!Try it!
  • 26. DOM Manipulation Attribute Creation $('<p class="foo" id="bar" style="color: red;">hello</p>'); [<p class="foo" id="bar" style="color: red;">hello</p>]​ ​ ​ ​ ​ ​ ​
  • 27. DOM Manipulation Attribute Creation $('p') .addClass('foo') .attr('id', 'bar') .css('color', 'red'); [<p class="foo" id="bar" style="color: red;">hello</p>]​ ​ ​ ​ ​ ​ ​ Try it!Try it!
  • 29. Data Storage Read data-* attributes from the DOM <div id="appointment1" data-date="2014-04-11">April 11, 2014</div> $('#appointment1').data('date'); "2014-04-11"
  • 30. Data Storage Read data-* attributes from the DOM <ul> <li data-price="100">Unordered list item 1</li> <li data-price="275">Unordered list item 2</li> <li data-price="50">Unordered list item 3</li> <li data-price="150">Unordered list item 4</li> </ul> $('ul li').data('price'); 100
  • 31. Data Storage Read data-* attributes from the DOM <ul> <li data-price="100">Unordered list item 1</li> <li data-price="275">Unordered list item 2</li> <li data-price="50">Unordered list item 3</li> <li data-price="150">Unordered list item 4</li> </ul> $('ul li').each(function (idx, el) { console.log($(el).data('price')); }); Try it!Try it!
  • 32. Data Storage Write data-* attributes to elements* <div id="appointment2">April 11, 2014</div> $('#appointment1').data('date', '2014-04-11'); *Cached internally. Not written to the DOM.
  • 33. Data Storage Write data-* attributes to elements* $('ol li').data('price', 100); *Cached internally. Not written to the DOM.
  • 35. Data Storage 1) Read the price data from the unordered list items 2) Add a 10% tax to each value 3) Write the newly taxed price to each ordered list counterpart $('ul li').each(function (idx, el) { var price = $(el).data('price') * 1.10; $('ol li').eq(idx).data('price', price); });
  • 37. Events Direct event handler assignment $('a').on('click', function (e) { e.preventDefault(); // do something });
  • 38. Events Direct event handler assignment with data $('a').on('click', {priority: 'high'}, function (e) { e.preventDefault(); console.log(e.data.priority); });
  • 39. Events Delegated event handler assignment $(document).on('click', 'a', function (e) { e.preventDefault(); // do something }); Faster. Future-friendly.
  • 40. Events Custom event $('.state').on('dataReady', function (e, payload) { $(this).addClass(payload.state); }); $('.state').trigger('dataReady', { state: 'pending' });
  • 41. Events Namespaced event $('a').on('click.loggedIn', function (e) { console.log('logged in click') }); $('a').on('mouseover.loggedIn', function (e) { console.log('logged in mouseover'); }); $('a').off('.loggedIn');
  • 42. Events Stopping propagation $('li').on('click', function (e) { console.log('li clicked!'); }); $('ul').on('click', function (e) { console.log('ul clicked!'); });
  • 43. Events Stopping propagation $('li').on('click', function (e) { e.stopPropagation(); console.log('li clicked!'); }); $('ul').on('click', function (e) { console.log('ul clicked!'); }); Try it!Try it!
  • 44. Events Using the right ready $(document).on('ready', function () { console.log('Will not fire if assigned late.'); }); $(document).ready(function () { console.log('Will fire even if assigned late.'); });
  • 46. Custom Selectors We saw: $('p:first'); Which is actually just: $.expr[':'].first = function (el) { … }
  • 47. Custom Selectors We can write whatever we want: $.expr[':'].inactive = function (el) { return $(el).hasClass('inactive'); }; Which we can then use: $('p:inactive'); [<p id="s1p2" class="inactive section1">Paragraph 2</p>,​ ​ ​ ​ <p id="s2p2" class="inactive section2">Paragraph 2</p>]​ ​ ​ ​ Try it!Try it!
  • 49. Building a Plugin $.fn.hideRemove = function () { return this.hide('slow', function () { $(this).remove(); }); }; Which we can then use: $('p').hideRemove(); Try it!Try it!
  • 50. Thank you! Happy to answer your questions throughout the conference and afterwards! ara.pehlivanian@gmail.com twitter.com/ara_p