Last Updated: September 09, 2019
·
1.459K
· 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);
};

Have a fresh tip? Share with Coderwall community!

Post
Post a tip
Did you know?

JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.

Security insights from Cside