SlideShare a Scribd company logo
jQuery Behaviours

  bind, live, livequery




   Richard Paul, Sep 2009
Unobstrusive Javascript

Clean separation of
   Data (HTML)
   Style (CSS)
   Behaviour (Javascript)
 ...
 <form id="myForm" onsubmit="doSomething()">
   ...
 </form>
 ...
 <script type="text/javascript">
   $('#myForm').submit(function() { /* do something */ });
 </script>
Javascript Events

In Javascript, events bubble up.
<body>
  <div>
    <button>A button</button>
  </div>
</body>

Clicking the button bubbles the event up (button > div > body)

However IE doesn't play nice with form events                  (select, change,
submit, reset) , they don't bubble (1).


http://www.quirksmode.org/js/events_order.html
(1) http://blogs.sun.com/greimer/entry/event_delegation_for_submit_change
Event Delegation

Event delegation is the practice of letting events bubble up to a
handling node.

$('table').click(function(event) {
                                            r1c1    r1c2   r1c3
  alert(event.target.innerHTML);
});
                                            r2c1    r2c2   r2c3




As the target always points to the element the event
happened on, clicking on any cell will result in its contents
being alerted.
Adding new rows

When adding new cells, no extra work is required.
Clicking a new row will bubble up to the table's click handler.

Demo

In practice you would want to check the element is of the
correct type/class etc.
Binding Mechanisms in jQuery

There are a number of ways of binding behaviour in jQuery


bind        $('form').bind('submit', function(){})

live        $('form').live('submit', function(){})

liveQuery   $('form').liveQuery('submit', function(){})



Commonly when using bind you would use the shortcut method:
$('form').submit(function(){})
Comparison Grid

                        Event
            Dynamic                IE6 Form Events   jQuery Core
                      Delegation



  bind        No         No             Yes             Yes




  live        Yes        Yes             No             Yes




liveQuery     Yes        No             Yes          No (plugin)
Facebox Use Case




Facebox uses jQuery's clone method to copy the required
content into its modal dialog.

Any behaviours initialised with bind aren't applied to the
facebox content.
Facebox with jQuery.live

Solution, use jQuery.live to apply behaviours using the event
delegation style.

$('form').live('submit', function() {
  // submit form via ajax
  return false;
});




                        Simple!
What about Internet Exploder?




Recall that IE doesn't bubble form events... like submit.

Using jQuery.live doesn't work for IE as the event doesn't
bubble so the handler is never invoked.

http://www.chigarden.com/2007/10/tutorial-making-the-ie-voodoo-doll/
liveQuery

We can't use jQuery.bind as the form is dynamically created (by
facebox's clone call).

Enter liveQuery

This plugin adds the handler directly to the form element.

No event delegation, and it works in IE6!

Demo

liveQuery uses some fancy tricks (async binding etc) to keep
your website performing.
Comments, queries, suggestions or theories?

More Related Content

What's hot (20)

PPT
JavaScript Libraries
Daminda Herath
 
PPTX
Events - Part 2
alexisabril
 
PDF
Yahoo presentation: JavaScript Events
Peter-Paul Koch
 
PDF
The Fine Art of JavaScript Event Handling
Yorick Phoenix
 
PPTX
The Real Story Behind JavaScript Performance on Mobile... Because Science!
Ryan J. Salva
 
PPTX
State, Life cycle, Methods & Events
NodeXperts
 
PPTX
Getting classy with ES6
Andy Sharman
 
PPT
Introduction to AngularJS
Anass90
 
PDF
[Osxdev]7.handoff
NAVER D2
 
PPTX
FuncUnit
Brian Moschel
 
PPTX
DHTML - Events & Buttons
Deep Patel
 
PPTX
Jquery introduce
Major Ye
 
PDF
DOT NET LAB PROGRAM PERIYAR UNIVERSITY
GOKUL SREE
 
DOCX
Bobcat Exchange Program
Bradley Robertson
 
PPTX
jQuery Has Coding Standards
RJ Bruneel
 
PDF
Introduction to jQuery
Nivedhitha Venugopal
 
PPT
User Switcher for Cascade Server
hannonhill
 
PDF
jQuery and Rails: Best Friends Forever
stephskardal
 
PPTX
Geb qa fest2017
Sviatkin Yaroslav
 
PDF
Vue.js for beginners
Julio Bitencourt
 
JavaScript Libraries
Daminda Herath
 
Events - Part 2
alexisabril
 
Yahoo presentation: JavaScript Events
Peter-Paul Koch
 
The Fine Art of JavaScript Event Handling
Yorick Phoenix
 
The Real Story Behind JavaScript Performance on Mobile... Because Science!
Ryan J. Salva
 
State, Life cycle, Methods & Events
NodeXperts
 
Getting classy with ES6
Andy Sharman
 
Introduction to AngularJS
Anass90
 
[Osxdev]7.handoff
NAVER D2
 
FuncUnit
Brian Moschel
 
DHTML - Events & Buttons
Deep Patel
 
Jquery introduce
Major Ye
 
DOT NET LAB PROGRAM PERIYAR UNIVERSITY
GOKUL SREE
 
Bobcat Exchange Program
Bradley Robertson
 
jQuery Has Coding Standards
RJ Bruneel
 
Introduction to jQuery
Nivedhitha Venugopal
 
User Switcher for Cascade Server
hannonhill
 
jQuery and Rails: Best Friends Forever
stephskardal
 
Geb qa fest2017
Sviatkin Yaroslav
 
Vue.js for beginners
Julio Bitencourt
 

Similar to jQuery Behaviours (20)

PDF
Introduction to jQuery
Nagaraju Sangam
 
PDF
Lec 5
maamir farooq
 
PDF
Frontin like-a-backer
Frank de Jonge
 
PPTX
JQuery Overview
Mahmoud Tolba
 
PPT
jQuery 1.7 Events
dmethvin
 
PPTX
Unit3.pptx
AnamikaRai59
 
PPT
Kick start with j query
Md. Ziaul Haq
 
PDF
jQuery
Ivano Malavolta
 
PPTX
A to Z about JQuery - Become Newbie to Expert Java Developer
Manoj Bhuva
 
PPTX
QA Fest 2017. Ярослав Святкин. Тестовый фреймворк GEB для тестирования WEB пр...
QAFest
 
PPTX
JQUERY TUTORIALS
Moize Roxas
 
PPTX
J Query The Write Less Do More Javascript Library
rsnarayanan
 
PPTX
Will your code blend? : Toronto Code Camp 2010 : Barry Gervin
Barry Gervin
 
PDF
jQuery - Chapter 3 - Effects
WebStackAcademy
 
PPTX
Client Web
Markiyan Matsekh
 
PPT
Digesting jQuery
Mindfire Solutions
 
PDF
Jquery
Gulbir Chaudhary
 
PPTX
J Query (Complete Course) by Muhammad Ehtisham Siddiqui
Muhammad Ehtisham Siddiqui
 
PPT
jQuery Fundamentals
Doncho Minkov
 
Introduction to jQuery
Nagaraju Sangam
 
Frontin like-a-backer
Frank de Jonge
 
JQuery Overview
Mahmoud Tolba
 
jQuery 1.7 Events
dmethvin
 
Unit3.pptx
AnamikaRai59
 
Kick start with j query
Md. Ziaul Haq
 
A to Z about JQuery - Become Newbie to Expert Java Developer
Manoj Bhuva
 
QA Fest 2017. Ярослав Святкин. Тестовый фреймворк GEB для тестирования WEB пр...
QAFest
 
JQUERY TUTORIALS
Moize Roxas
 
J Query The Write Less Do More Javascript Library
rsnarayanan
 
Will your code blend? : Toronto Code Camp 2010 : Barry Gervin
Barry Gervin
 
jQuery - Chapter 3 - Effects
WebStackAcademy
 
Client Web
Markiyan Matsekh
 
Digesting jQuery
Mindfire Solutions
 
J Query (Complete Course) by Muhammad Ehtisham Siddiqui
Muhammad Ehtisham Siddiqui
 
jQuery Fundamentals
Doncho Minkov
 
Ad

More from Richard Paul (9)

PDF
Cucumber on the JVM with Groovy
Richard Paul
 
PDF
Acceptance testing with Geb
Richard Paul
 
PDF
Acceptance tests
Richard Paul
 
PDF
Introduction to Spring's Dependency Injection
Richard Paul
 
PDF
Introduction to Spring MVC
Richard Paul
 
PDF
Unit Testing Fundamentals
Richard Paul
 
PDF
Using Dojo
Richard Paul
 
PDF
Javascript & Ajax Basics
Richard Paul
 
PDF
Mocking in Java with Mockito
Richard Paul
 
Cucumber on the JVM with Groovy
Richard Paul
 
Acceptance testing with Geb
Richard Paul
 
Acceptance tests
Richard Paul
 
Introduction to Spring's Dependency Injection
Richard Paul
 
Introduction to Spring MVC
Richard Paul
 
Unit Testing Fundamentals
Richard Paul
 
Using Dojo
Richard Paul
 
Javascript & Ajax Basics
Richard Paul
 
Mocking in Java with Mockito
Richard Paul
 
Ad

Recently uploaded (20)

PPTX
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
Biography of Daniel Podor.pdf
Daniel Podor
 
PDF
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
PDF
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
PPTX
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
PDF
July Patch Tuesday
Ivanti
 
PDF
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
PDF
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
PDF
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
PDF
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
PDF
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PDF
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
PDF
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
PDF
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PPTX
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 
Q2 FY26 Tableau User Group Leader Quarterly Call
lward7
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
Biography of Daniel Podor.pdf
Daniel Podor
 
The Rise of AI and IoT in Mobile App Tech.pdf
IMG Global Infotech
 
Building Real-Time Digital Twins with IBM Maximo & ArcGIS Indoors
Safe Software
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
Webinar: Introduction to LF Energy EVerest
DanBrown980551
 
July Patch Tuesday
Ivanti
 
IoT-Powered Industrial Transformation – Smart Manufacturing to Connected Heal...
Rejig Digital
 
Newgen 2022-Forrester Newgen TEI_13 05 2022-The-Total-Economic-Impact-Newgen-...
darshakparmar
 
Bitcoin for Millennials podcast with Bram, Power Laws of Bitcoin
Stephen Perrenod
 
Agentic AI lifecycle for Enterprise Hyper-Automation
Debmalya Biswas
 
"AI Transformation: Directions and Challenges", Pavlo Shaternik
Fwdays
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
Fl Studio 24.2.2 Build 4597 Crack for Windows Free Download 2025
faizk77g
 
From Code to Challenge: Crafting Skill-Based Games That Engage and Reward
aiyshauae
 
Achieving Consistent and Reliable AI Code Generation - Medusa AI
medusaaico
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
AUTOMATION AND ROBOTICS IN PHARMA INDUSTRY.pptx
sameeraaabegumm
 

jQuery Behaviours

  • 1. jQuery Behaviours bind, live, livequery Richard Paul, Sep 2009
  • 2. Unobstrusive Javascript Clean separation of Data (HTML) Style (CSS) Behaviour (Javascript) ... <form id="myForm" onsubmit="doSomething()"> ... </form> ... <script type="text/javascript"> $('#myForm').submit(function() { /* do something */ }); </script>
  • 3. Javascript Events In Javascript, events bubble up. <body> <div> <button>A button</button> </div> </body> Clicking the button bubbles the event up (button > div > body) However IE doesn't play nice with form events (select, change, submit, reset) , they don't bubble (1). http://www.quirksmode.org/js/events_order.html (1) http://blogs.sun.com/greimer/entry/event_delegation_for_submit_change
  • 4. Event Delegation Event delegation is the practice of letting events bubble up to a handling node. $('table').click(function(event) { r1c1 r1c2 r1c3 alert(event.target.innerHTML); }); r2c1 r2c2 r2c3 As the target always points to the element the event happened on, clicking on any cell will result in its contents being alerted.
  • 5. Adding new rows When adding new cells, no extra work is required. Clicking a new row will bubble up to the table's click handler. Demo In practice you would want to check the element is of the correct type/class etc.
  • 6. Binding Mechanisms in jQuery There are a number of ways of binding behaviour in jQuery bind $('form').bind('submit', function(){}) live $('form').live('submit', function(){}) liveQuery $('form').liveQuery('submit', function(){}) Commonly when using bind you would use the shortcut method: $('form').submit(function(){})
  • 7. Comparison Grid Event Dynamic IE6 Form Events jQuery Core Delegation bind No No Yes Yes live Yes Yes No Yes liveQuery Yes No Yes No (plugin)
  • 8. Facebox Use Case Facebox uses jQuery's clone method to copy the required content into its modal dialog. Any behaviours initialised with bind aren't applied to the facebox content.
  • 9. Facebox with jQuery.live Solution, use jQuery.live to apply behaviours using the event delegation style. $('form').live('submit', function() { // submit form via ajax return false; }); Simple!
  • 10. What about Internet Exploder? Recall that IE doesn't bubble form events... like submit. Using jQuery.live doesn't work for IE as the event doesn't bubble so the handler is never invoked. http://www.chigarden.com/2007/10/tutorial-making-the-ie-voodoo-doll/
  • 11. liveQuery We can't use jQuery.bind as the form is dynamically created (by facebox's clone call). Enter liveQuery This plugin adds the handler directly to the form element. No event delegation, and it works in IE6! Demo liveQuery uses some fancy tricks (async binding etc) to keep your website performing.