0

Is there a way in JavaScript that I can override an event, but still call the original event's code? I basically want to add a single line of code to the end. So for example:

Original function

function DropDownList_OnFocus(a, b) {
    // Original Code
}

What I'd need

function DropDownList_OnFocus(a, b) {
    // Original Code
    // New Code        
}
Scott Hulme
  • 124
  • 8
  • so you want to update an event handler not the event itself? why not simply add another event handler for the same event? – gurvinder372 Jan 29 '16 at 11:44
  • Hi @gurvinder372, that's correct, however I need to ensure that the additional code is executed after the original event. I think adding another event handler could have unpredictable results in some cases. – Scott Hulme Jan 29 '16 at 11:50
  • 1
    unless your event handlers have asynch processing, they will be executed in the order of addition to the event http://stackoverflow.com/questions/18821625/how-do-event-handlers-in-javascript-get-processed-by-the-event-loop – gurvinder372 Jan 29 '16 at 11:52

2 Answers2

0

Event handlers always execute sequentially in the order they were bound.

So You can simply add another event handler for the same event on the same element and put more/remaining code in the second event handler.

Mukesh Saini
  • 1,388
  • 1
  • 7
  • 16
0

I've managed to get it working using the following:

var base = DropDownList.OnFocus;
var DropDownOnFocusOverride = (function () {
    return function () {
        base();
        // Additional Code
    }
});

DropDownList.OnFocus = DropDownOnFocusOverride();
Scott Hulme
  • 124
  • 8