-1

I am new to JQuery and learning it. I found that the JQuery .on() method can be used in place of .bind(), .live() and .delegate() methods. I would like to know how it can work like all the three mentioned methods and what are the pros/cons of doing so?

Chaitanya
  • 290
  • 2
  • 5
  • 12

3 Answers3

1

See below example

// Bind
$( "#members li a" ).on( "click", function( e ) {} );
$( "#members li a" ).bind( "click", function( e ) {} );

// Live
$( document ).on( "click", "#members li a", function( e ) {} );
$( "#members li a" ).live( "click", function( e ) {} );

// Delegate
$( "#members" ).on( "click", "li a", function( e ) {} );
$( "#members" ).delegate( "li a", "click", function( e ) {} );

For Reference

http://www.elijahmanor.com/differences-between-jquery-bind-vs-live-vs-delegate-vs-on/

rajesh kakawat
  • 10,330
  • 1
  • 18
  • 38
0

The .on() method attaches event handlers to the currently selected set of elements in the jQuery object. As of jQuery 1.7, the .on() method provides all functionality required for attaching event handlers. For help in converting from older jQuery event methods, see .bind(), .delegate(), and .live(). (Quote from jQuery .on page.)

Nick
  • 5,767
  • 10
  • 50
  • 77
-1

The mainly pros is on() work in dynamic elements (instead of live()).

Read more: http://api.jquery.com/on/

kicaj
  • 2,633
  • 4
  • 40
  • 62