I am tryign to override the JQuery’s .show and .hide methods to launch trigger events before and after they are called with the following code.
$(document).ready(function () {
$('#dataBox').bind('afterShow', function () {
alert('afterShow');
});
$('#dataBox').bind('afterHide', function () {
alert('afterHide');
});
$('#dataBox').bind('beforeShow', function () {
alert('beforeShow');
});
$('#dataBox').bind('beforeHide', function () {
alert('beforeHide');
});
$('#toggleButton').click(function(){
if($('#dataBox').is(':visible')) {
$('#dataBox').hide ();
} else {
$('#dataBox').show();
}
});
});
jQuery(function ($) {
var _oldShow = $.fn.show;
//Override jquery's 'show()' method to include two triggered events before and after
$.fn.show = function (speed, oldCallback) {
return $(this).each(function () {
var obj = $(this),
newCallback = function () {
if ($.isFunction(oldCallback)) {
oldCallback.apply(obj);
}
obj.trigger('afterShow');
};
obj.trigger('beforeShow');
_oldShow.apply(obj, [speed, newCallback]);
});
}
});
jQuery(function ($) {
var _oldHide = $.fn.hide;
//Override jquery's 'hide()' method to include two triggered events before and after
$.fn.hide = function (speed, oldCallback) {
return $(this).each(function () {
var obj = $(this),
newCallback = function () {
if ($.isFunction(oldCallback)) {
oldCallback.apply(obj);
}
obj.trigger('afterHide');
};
obj.trigger('beforeHide');
_oldHide.apply(obj, [speed, newCallback]);
});
}
});
I have the following Markup:
<input type='text' id='dataBox'/>
<input type='button' value='toggle' id='toggleButton' />
When I click the toggle button the ‘beforeHide’ and ‘beforeShow’ events are triggering while the ‘afterShow’ and ‘afterHide’ aren’t. Can anyone clue me in as to what I am doing wrong?
plz check the demo fiddle . hope it works
$.show / $.hide – override function:
usage