@ Unicron had the correct answer, but he did not give a good example. Check this:
$( 'tr.onCall' ).on( 'click', function( event ) { let pEvent = function() { return event; }
By declaring a pEvent function inside an anonymous function that fires on("click") , the event object is "frozen" (encapsulated) in its original context. Even when you call it in another context of the ajax success function, it retains its original context.
A more specific example: I'm going to open a modal dialog box (in the style of Div) by clicking, but when the dialog box is closed, I want to return focus to the element that was clicked to open it first ...
$( 'tr.onCall' ).on( 'click', function( event ) { let rTarget = function() { return event.currentTarget; } $.ajax( { url: 'ajax_data.php', ...other settings... success: function( data ) { modal_dialog( data, { returnTarget: rTarget(), ...other settings... } ); } }); });
If successful, it calls the user-defined function modal_dialog() (defined elsewhere), passing an object containing various settings. The returnTarget parameter contains the HTML attribute of the element that was clicked; so when I close the dialog, I can run $(options.returnTarget).focus(); return focus to this element.
source share