I have a jQuery event handler function that is triggered on click of two buttons - Positive and Negative. In either case, I pass a different data object to the handler (as below).
I have a AJAX POST within this event handler. However I am unable to access the on click event's data inside the AJAX POST handler.
$('#Positive').on("click", {agree:true}, handlerFunc);
$('#Negative').on("click", {agree:false}, handlerFunc);
function handlerFunc(e){
e.preventDefault();
//e.data is valid here
if (e.data.agree === true){
//if condition is valid and it enters this block
startTimer();
}
$.post(url_, postData, function(data){
//console.log(e.data);
//e.data is undefined at this point
if (e.data.agree === false){
return true;
}
//do other stuff
});
}
Why is e.data undefined within the AJAX POST?
I created a new variable to store the e.data.agree value and it works as expected.
$('#Positive').on("click", {agree:true}, handlerFunc);
$('#Negative').on("click", {agree:false}, handlerFunc);
function handlerFunc(e){
e.preventDefault();
//e.data is valid here
var agree_data = e.data.agree;
if (e.data.agree === true){
//if condition is valid and it enters this block
startTimer();
}
$.post(url_, postData, function(data){
//console.log(agree_data);
//agree_data is valid at this point
if (agree_data === false){
return true;
}
//do other stuff
});
}
Why is e.data undefined in the first case - within the AJAX POST? Does it get overwritten?
NOTE: I have two different environments and it seems to work fine in the test environment but gives the undefined error in production.
startTimer();code looks like