Oliver Kuchies Oliver Kuchies - 3 months ago 18
Javascript Question

Run a Javascript function after a default jQuery function is run

in jQuery; How would I execute a function once a specific function is run. For example; everytime

is run, can I run a function no-matter the context?

In this specific example i'm looking to listen to
, reason being I need to re-register event listeners when new elements are placed on the screen. (i.e. btn click event listeners).

How can I accomplish this? Let us use
as an example function.


Probably the right way to do what you want is with event delegation, as described in Event binding on dynamically created elements?. E.g. change

$(".search-btn").on("click", function() {


$("#container").on("click", ".search-btn", function() {

Replace #container with a selector for a static element that contains all the dynamically-added search buttons.

But to show how to do what you asked, you can do:

(function(oldappend) {
    $.fn.append = function() {
        var result = oldappend.apply(this, arguments);
        return result;

This saves the original value of the jQuery .append() method in the local variable oldappend, then redefines it to a function that calls the original function and then calls your function.