I want to keep a reference of the button click function, do my stuff, then run the original click function.
I know usually I can do such things in document.ready, e.g:
var originalOnClick = $("#myBtn").click;
$("#myBtn").click = function() {
// do my staff here, and only then call the original:
originalOnClick();
}
(please correct this code if it is wrong!)
but, I have a special case. in my page onLoad function, there are asynchronous ajax calls. so functions called on document.ready are actually called before the ajax code was finished.
Is there a way to change the click event when calling click() or onclick() ? I mean, is there an event that is called immediatly before\after the click event?
or that I must ask the user to click a (I can see that this page is loaded) button, and only then I can do my staff ?
EDIT 1: The ajax code is in an included script. I cannot change it.
EDIT 2: Possible solution: hide the original button, but use its event:
HTML:
<div class="object1" style="display: none;" >original button.</div>
<div class="object2">my button.</div>
JS:
function doMyStuff() {
alert(' doMyStuff .');
}
$(document).ready(function(){
$('.object1').click(function(){
alert('original staff. ');
}); // actually, this is just a test code. In my project, I cannot edit this function
$('.object2').click(function() {
doMyStuff();
$('.object1').click();
});
});
clickmethod of one particular element? Replacing the method will only affect that specific jQuery object. When you later on retrieve the same element with$("#myBtn"), it will return a new jQuery object without your previous changes. Please elaborate more about what you're intending to do with this, so we can get a better understanding and suggest better alternatives.click? Or are those values coming from a<form>being submitted by clicking on an<input type="submit">?