This is best when you need to add newly created content, bind events in a document, for a given selector, or for custom events.
For example, if you are trying to attach a click to a given class. "Our-class ":
$('.your-class').on('click', function (e) {
You would prefer to replace it with:
$(document).on('click', '.your-class', function (e) {
This is exactly the same for jQuery UI:
You would simply prefer to replace:
// Here you just bind a click on the existing elements of your class this._on('.categoryNavigationExpandIcon', { click: function(event) { var $container = $(event.target).parent(); if( ($container).data('expanded') === false ) { this._expand($container); } else { this._contract($container); } } });
By:
// Bind a custom event on the document in order to delegate this event event to the new created classes this._on(this.document, { 'click.categoryNavigationExpandIcon': function(event) { var $container = $(event.target).parent(); if( ($container).data('expanded') === false ) { this._expand($container); } else { this._contract($container); } } });
You can find the updated JSFiddle here: http://jsfiddle.net/vEAhq/7/
By the way, you can skip the closing tag when creating an element using jQuery!
$('<div>'); // instead of $('<div></div>');
source share