Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery .on() instead of .delegate() without event

Is it possible to use jquery .on() method instead of .delegate() when there is no event to be listened to?

According to the .on() documentation:

.on( events [, selector] [, data] , handler(eventObject) )

The events argument is not optional.

The use of .on()/.delegate() is for elements that are added dynamically.

like image 609
KoU_warch Avatar asked Aug 09 '12 14:08

KoU_warch


1 Answers

You can use custom events:(it IS still an event, but YOUR event )

markup:

<div id='mePlease'>
 <div id='noWay'>Hi</div>
</div>

$('#mePlease').on('wacky','#noWay',function(){
   alert('wackyEnough');
});
$('#noWay').trigger('wacky');

but really, this could be done with a simple function call.

like image 105
Mark Schultheiss Avatar answered Oct 20 '22 00:10

Mark Schultheiss