- Jquery provides a special type of objects called callbacks . This object can store method and fire this methods to a specific events.
- In this demo, “callbackMethod1 and callbackMethod2 is added two Jquery callback objects and fired on mouse enter and leave method”.
- The Jquery code for this is,
$(document).ready(function () {
var callbacks = $.Callbacks(),
callbackMethod1 = function () {
$('.my-name').append("<li class='green'>CallBack Method 1 is Fired</li>")
},
callbackMethod2 = function () {
$('.my-name').append("<li class='yellow'>CallBack Method 2 is Fired</li>")
};
callbacks.add(callbackMethod1);
callbacks.add(callbackMethod2);
$(".start-name").hover(
function () {
callbacks.fire("callbackMethod1");
},
function () {
callbacks.fire("callbackMethod2");
});
})
- The Jsfiddle code
- The Console of callbacks objects,
- The Output of the code screenshot,
- Output in fiddle,