SlideShare a Scribd company logo
jQuery Events
  by: Charlie Choiniere
Event Models
• There are three event models: Bubble, Capture,
  and W3C.
• Bubbling is the only one that currently works in
  all browsers.
• Bubbling starts at the inner most element and
  travels up ancestor elements until it reaches an
  event listener.

                  http://www.quirksmode.org/js/events_order.html
Bubbling Event Order


        2nd



        1st
.bind()
http://api.jquery.com/bind/
Usage:
$(‘.foo’).bind(‘click’, function({
	 console.log(“OH HAI!”);
});
click      click      click      click      click

.foo       .foo       .foo       .foo       .foo
   click      click      click      click      click

.foo       .foo       .foo       .foo       .foo
   click      click      click      click      click

.foo       .foo       .foo       .foo       .foo
   click      click      click      click      click

.foo       .foo       .foo       .foo       .foo
   click      click      click      click      click

.foo       .foo       .foo       .foo       .foo
Notes for .bind()
• Bind attaches events directly to each element that
  matches the selector.
• Lower overhead when processing events.
• Higher memory usage by attaching many events.
• New DOM elements are not setup automatically
  and need to have .bind() applied each time one is
  added to the page.
.live()
http://api.jquery.com/live/
Usage:
$(‘.foo’).live(‘click’, function({
	 console.log(“OH HAI!”);
});
document

.foo   .foo     .foo     .foo   click


.foo   .foo     .foo     .foo   .foo

.foo   .foo     .foo     .foo   .foo

.foo   .foo     .foo     .foo   .foo

.foo   .foo     .foo     .foo   .foo
Notes for .live()
• live() always attaches events to the document.
• Bubbling has to travel all the way up to the top of
  the document for every event.
• Higher processing because live needs to check if
  the event matches the given selector.
• Elements added to the DOM dynamically will still
  be handled by the .live() event handler.
• Don’t use live().
.delegate()
http://api.jquery.com/delegate/
Usage:
$(‘.container’)
	 .delegate(‘.foo’, ‘click’, function({
	 	 console.log(“OH HAI!”);
	 });
document
.container
                       .bar     .bar   .bar
             click
.foo
                       .bar     .bar   .bar
 .foo        .foo
                       .bar     .bar   .bar
 .foo        .foo
                       .bar     .bar   .bar
 .foo        .foo
                       .bar     .bar   .bar
 .foo        .foo
Notes for .delegate()
• A localized version of .live() events.
• Events are attached to a containing element
  instead of the document.
• Higher performance than live() due to the shorter
  path of the event Bubble.
• Always use instead of .live() to avoid unnecessary
  bubbling overhead.
$(‘.wax’).on();
$(‘.wax’).off();
  http://api.jquery.com/on/
  http://api.jquery.com/off/
Bind Replacement
        Usage:
$(‘.foo’).on(‘click’, function({
	 console.log(“OH HAI!”);
});
Live Replacement
         Usage:
$(document)
	 .on(‘click’, ‘.foo’, function({
	 	 console.log(“OH HAI!”);
	 });
Delegate Replacement
       Usage:
$(‘.container’)
	 .on(‘click’, ‘.foo’, function({
	 	 console.log(“OH HAI!”);
	 });
Notes for .on()
• New in jQuery 1.7
• Replacement for the 3 separate event APIs by
  providing a unified API for all event types.
• .bind(), .live(), and .delegate will remain in jQuery
  for backwards compatibility
• Internally .bind(), .live(), and .delegate now use
  the .on() API
• Performance; Enhance, Enhance, Enhance.

More Related Content

What's hot

JavaScript APIs - The Web is the Platform - MozCamp, Buenos Aires
JavaScript APIs - The Web is the Platform - MozCamp, Buenos AiresJavaScript APIs - The Web is the Platform - MozCamp, Buenos Aires
JavaScript APIs - The Web is the Platform - MozCamp, Buenos Aires
Robert Nyman
 
Hands on Pier
Hands on PierHands on Pier
Hands on Pier
ESUG
 
JavaScript APIs - The Web is the Platform - MDN Hack Day, Montevideo
JavaScript APIs - The Web is the Platform - MDN Hack Day, MontevideoJavaScript APIs - The Web is the Platform - MDN Hack Day, Montevideo
JavaScript APIs - The Web is the Platform - MDN Hack Day, Montevideo
Robert Nyman
 
Solving Problems with YUI3: AutoComplete
Solving Problems with YUI3: AutoCompleteSolving Problems with YUI3: AutoComplete
Solving Problems with YUI3: AutoComplete
IsaacSchlueter
 
The Fine Art of JavaScript Event Handling
The Fine Art of JavaScript Event HandlingThe Fine Art of JavaScript Event Handling
The Fine Art of JavaScript Event Handling
Yorick Phoenix
 
JavaScript: Events Handling
JavaScript: Events HandlingJavaScript: Events Handling
JavaScript: Events Handling
Yuriy Bezgachnyuk
 
The Open Web and what it means
The Open Web and what it meansThe Open Web and what it means
The Open Web and what it means
Robert Nyman
 
Memory Leaks In Internet Explorer
Memory Leaks In Internet ExplorerMemory Leaks In Internet Explorer
Memory Leaks In Internet Explorer
Christopher Blum
 

What's hot (8)

JavaScript APIs - The Web is the Platform - MozCamp, Buenos Aires
JavaScript APIs - The Web is the Platform - MozCamp, Buenos AiresJavaScript APIs - The Web is the Platform - MozCamp, Buenos Aires
JavaScript APIs - The Web is the Platform - MozCamp, Buenos Aires
 
Hands on Pier
Hands on PierHands on Pier
Hands on Pier
 
JavaScript APIs - The Web is the Platform - MDN Hack Day, Montevideo
JavaScript APIs - The Web is the Platform - MDN Hack Day, MontevideoJavaScript APIs - The Web is the Platform - MDN Hack Day, Montevideo
JavaScript APIs - The Web is the Platform - MDN Hack Day, Montevideo
 
Solving Problems with YUI3: AutoComplete
Solving Problems with YUI3: AutoCompleteSolving Problems with YUI3: AutoComplete
Solving Problems with YUI3: AutoComplete
 
The Fine Art of JavaScript Event Handling
The Fine Art of JavaScript Event HandlingThe Fine Art of JavaScript Event Handling
The Fine Art of JavaScript Event Handling
 
JavaScript: Events Handling
JavaScript: Events HandlingJavaScript: Events Handling
JavaScript: Events Handling
 
The Open Web and what it means
The Open Web and what it meansThe Open Web and what it means
The Open Web and what it means
 
Memory Leaks In Internet Explorer
Memory Leaks In Internet ExplorerMemory Leaks In Internet Explorer
Memory Leaks In Internet Explorer
 

Viewers also liked

LINQ
LINQLINQ
REAL Solutions_Pre-Paid Debit Handout_ILCUL
REAL Solutions_Pre-Paid Debit Handout_ILCULREAL Solutions_Pre-Paid Debit Handout_ILCUL
REAL Solutions_Pre-Paid Debit Handout_ILCUL
realsolutions
 
Presentasi Mas Anggit
Presentasi Mas AnggitPresentasi Mas Anggit
Presentasi Mas Anggit
ardikoro
 
Fiddler Callbacks
Fiddler CallbacksFiddler Callbacks
Fiddler Callbacks
katiehahn22
 
SIG pendidikan sebagai penunjang perencanaan pembangunan pendidikan
SIG pendidikan sebagai penunjang perencanaan pembangunan pendidikanSIG pendidikan sebagai penunjang perencanaan pembangunan pendidikan
SIG pendidikan sebagai penunjang perencanaan pembangunan pendidikan
Rinda Cahyana
 
ASP.NET - WebParts
ASP.NET - WebPartsASP.NET - WebParts
ASP.NET - WebParts
Leonardo Lourenço Silva
 

Viewers also liked (6)

LINQ
LINQLINQ
LINQ
 
REAL Solutions_Pre-Paid Debit Handout_ILCUL
REAL Solutions_Pre-Paid Debit Handout_ILCULREAL Solutions_Pre-Paid Debit Handout_ILCUL
REAL Solutions_Pre-Paid Debit Handout_ILCUL
 
Presentasi Mas Anggit
Presentasi Mas AnggitPresentasi Mas Anggit
Presentasi Mas Anggit
 
Fiddler Callbacks
Fiddler CallbacksFiddler Callbacks
Fiddler Callbacks
 
SIG pendidikan sebagai penunjang perencanaan pembangunan pendidikan
SIG pendidikan sebagai penunjang perencanaan pembangunan pendidikanSIG pendidikan sebagai penunjang perencanaan pembangunan pendidikan
SIG pendidikan sebagai penunjang perencanaan pembangunan pendidikan
 
ASP.NET - WebParts
ASP.NET - WebPartsASP.NET - WebParts
ASP.NET - WebParts
 

Recently uploaded

Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
MichaelKnudsen27
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
tolgahangng
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
Tatiana Kojar
 
Recommendation System using RAG Architecture
Recommendation System using RAG ArchitectureRecommendation System using RAG Architecture
Recommendation System using RAG Architecture
fredae14
 
WeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation TechniquesWeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation Techniques
Postman
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
Jakub Marek
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
innovationoecd
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
saastr
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
Brandon Minnick, MBA
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
Octavian Nadolu
 
Ocean lotus Threat actors project by John Sitima 2024 (1).pptx
Ocean lotus Threat actors project by John Sitima 2024 (1).pptxOcean lotus Threat actors project by John Sitima 2024 (1).pptx
Ocean lotus Threat actors project by John Sitima 2024 (1).pptx
SitimaJohn
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Malak Abu Hammad
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
Pixlogix Infotech
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
shyamraj55
 
Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
Zilliz
 
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdfMonitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Tosin Akinosho
 
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial IntelligenceAI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
IndexBug
 
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying AheadDigital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Wask
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
ssuserfac0301
 
5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides
DanBrown980551
 

Recently uploaded (20)

Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
 
Serial Arm Control in Real Time Presentation
Serial Arm Control in Real Time PresentationSerial Arm Control in Real Time Presentation
Serial Arm Control in Real Time Presentation
 
Skybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoptionSkybuffer SAM4U tool for SAP license adoption
Skybuffer SAM4U tool for SAP license adoption
 
Recommendation System using RAG Architecture
Recommendation System using RAG ArchitectureRecommendation System using RAG Architecture
Recommendation System using RAG Architecture
 
WeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation TechniquesWeTestAthens: Postman's AI & Automation Techniques
WeTestAthens: Postman's AI & Automation Techniques
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
 
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
Deep Dive: AI-Powered Marketing to Get More Leads and Customers with HyperGro...
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
 
Ocean lotus Threat actors project by John Sitima 2024 (1).pptx
Ocean lotus Threat actors project by John Sitima 2024 (1).pptxOcean lotus Threat actors project by John Sitima 2024 (1).pptx
Ocean lotus Threat actors project by John Sitima 2024 (1).pptx
 
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdfUnlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
Unlock the Future of Search with MongoDB Atlas_ Vector Search Unleashed.pdf
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
 
Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
 
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdfMonitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdf
 
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial IntelligenceAI 101: An Introduction to the Basics and Impact of Artificial Intelligence
AI 101: An Introduction to the Basics and Impact of Artificial Intelligence
 
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying AheadDigital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying Ahead
 
Taking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdfTaking AI to the Next Level in Manufacturing.pdf
Taking AI to the Next Level in Manufacturing.pdf
 
5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides5th LF Energy Power Grid Model Meet-up Slides
5th LF Energy Power Grid Model Meet-up Slides
 

jQuery Events

Editor's Notes

  1. \n
  2. \n
  3. \n
  4. \n
  5. \n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. \n
  15. \n
  16. \n
  17. \n
  18. \n
  19. \n
  20. \n