6

I've been trying to work out how to pass additional parameters to a javascript callback function.

In similar posts users have succeeded using anonymous function (these are new to me so I may have been doing them wrong) but I just can't get them to fire.

The below is what I have now, I need to be able to pass the itemId to the function "ajaxCheck_Callback" as well as the response.

Hope this makes sense.

Thanks for the help.

function delete(itemId)
{
  selectpage.ajaxCheck(itemId, ajaxCheck_Callback);
}


Function ajaxCheck_Callback(response)

{
  alert(response);
  alert(itemId);
}

Thanks for the help guys. I now get undefined on my alert whereas previously this was alerting correctly.

function delete(itemId)
{
  selectpage.ajaxCheck(itemid, function () {ajaxCheck_Callback(itemid); });
}


function ajaxCheck_Callback(response)

{
  alert(response);
  alert(itemId);
}
JIbber4568
  • 839
  • 4
  • 16
  • 33

3 Answers3

12

The way is usually done is to use an anonymous function.

function deleteItem(itemId) {
    selectpage.ajaxCheck(itemId, function() { ajaxcheck_Callback(itemId); });
}

function ajaxCheck_Callback(response)
{
    alert(response);
}

Careful with your syntax, there are some errors in there. The function keyword has a lower-case f and you can't have a function named delete, that's a reserved keyword.

Alex Turpin
  • 46,743
  • 23
  • 113
  • 145
  • Good point about not using `delete` as a function name. (+1) – Matt Ball Dec 02 '11 at 16:05
  • Thanks for this. This is what I was trying before but doesn't seem to work. What I posted originally gives a successful alert for response. When I add in the anonymous function I then get an undefined alert for response. – JIbber4568 Dec 02 '11 at 16:17
  • Please post your exact code (if possible, reproduce the problem in http://jsfiddle.net) – Alex Turpin Dec 02 '11 at 16:18
  • Thanks. I have updated original question with current code. – JIbber4568 Dec 02 '11 at 16:33
  • @JIbber4568 rename your function `delete` to something else. – Alex Turpin Dec 02 '11 at 16:47
  • @Xeon06 Sorry, I have already renamed it to "ItemDelete" just didn't copy it into the new code. ;-) – JIbber4568 Dec 02 '11 at 16:51
  • @JIbber4568 well everything seems in order. Maybe it has to do with your `ajaxCheck` function. Without a full example on [jsFiddle](http://jsfiddle.net) there's no way of telling what's wrong. – Alex Turpin Dec 02 '11 at 16:55
  • Ok, thanks. Was a bit strange that the response came through before but then not with the anon function. I've never used jsFiddle but will take a look and see if I can get something setup on there. Cheers – JIbber4568 Dec 02 '11 at 17:08
3

Create an anonymous function, which calls your callback, and supplies the needed parameter.

function deleteItemById(itemId)
{
  selectpage.ajaxCheck(itemId, function() { ajaxCheck_Callback(itemId); });
}

The anonymous function will create a closure over itemId, and therefore will still have access to this value even after the original call to delete has long since ended.

For completeness, also note that delete is a reserved word in JavaScript; you need to name this function something else.

Adam Rackis
  • 82,527
  • 56
  • 270
  • 393
3

As your question mentions, you can do it using an anonymous function:

function deleteItem(itemId)
{
  selectpage.ajaxCheck(itemId, function ()
  {
      ajaxCheck_Callback(itemId);
  });
}

// Note, lowercase 'f' in 'function'
function ajaxCheck_Callback(response)
{
  alert(response);
}

It can also be done using a named function, which you might find more readable:

function deleteItem(itemId)
{
    function onAjaxCheck()
    {
        ajaxCheck_Callback(itemId);
    }

  selectpage.ajaxCheck(itemId, onAjaxCheck);
}

N.B. as @Xeon06 points out, delete is a reserved word, so that is not a valid function name.

Community
  • 1
  • 1
Matt Ball
  • 354,903
  • 100
  • 647
  • 710