Last Updated: September 09, 2019
·
959
· ihcsim

Simple Examples on Passing Paramaters Into Callback

(Tested at http://jsfiddle.net/ivan_sim/BYQTY/)

Example 1- Via Anonymous Callback

$("#example").click(function() {
    echoCallbackParams(params);
});
function echoCallbackParams(params) {
    console.log(params);
}

Example 2- Via predefined jQuery API (.click and .ajaxSuccess)

$("#example").click(params, echoCallbackEventParams);
function echoCallbackEventParams(params) {
    console.log(params.data);
}

$("#example").click(function() {
    $.getJSON("/echo/jsonp/", params, echoSuccess);
});
function echoSuccess(params, textStatus, jqXHR) {
    console.log("Text status: " + textStatus);
    console.log(params;
    console.log(jqXHR);
};

Example 3- Via Named Callback

fetchData(getJsonData, "/echo/jsonp/", params);
function fetchData(dataAccess, url, params) {
    dataAccess(url, params, echoSuccess);
};
function getJsonData(url, params, onSuccess) {
    $.getJSON(url, params, onSuccess);
};