0

I used to have a button like this:

<input type="button" value="Save" onclick="SaveReleaseStage(this, 'Reassigned', 'ReleaseStage/ReassignProcess');" />

where the "this" being passed was the button. Everything worked great.

When I changed it to this:

<input type="button" value="Save" onclick="ShowConfirmation('Confirm', 'Some message...', function () { SaveReleaseStage(this, 'Reassigned', 'ReleaseStage/ReassignProcess'); });" />

the "this" is now the Window object and not the button.

Is there a way to pass in the button to the callback without resorting to adding an ID and using getElementById or jQuery?

mclaassen
  • 5,018
  • 4
  • 30
  • 52

1 Answers1

3

Because it's an anonymous function with no binding context, and therefore this defaults to the global object. In your case that would be window.

Further reading about this behavior here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this

UPDATE: Per your question edit, you can pass in a closure reference to the button:

<input type="button" value="Save" onclick="var self=this; ShowConfirmation('Confirm', 'Some message...', function () { SaveReleaseStage(self, 'Reassigned', 'ReleaseStage/ReassignProcess'); });" />

Demo: http://jsfiddle.net/8caMT/

AlienWebguy
  • 76,997
  • 17
  • 122
  • 145