Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

overriding jQuery.find() function to provide extra functionality

I want to override jQuery.find to override extra functionality. I have used the idea discussed on Ben Nadel's blog to do so ( http://www.bennadel.com/blog/1624-Ask-Ben-Overriding-Core-jQuery-Methods.htm ) but for some reason it doesn't work for $.find(), this is my code

        (function () {
        // Store a reference to the original remove method.
        var originalFindFunction = jQuery.find;

        // Define overriding method.
        jQuery.find = function () {
            // Log that we are calling the overridden function.
            Console.log("------> Find method called");

            // then execute the original method
            var results = originalFindFunction.apply(this, arguments);
            return results;
        };
    })();

do you have an idea what's wrong, or how can I override a jquery function?

like image 882
kabaros Avatar asked Jul 26 '26 01:07

kabaros


1 Answers

The demonstration in your reference uses a function from .fn.

jQuery.fn.remove

Where you're overriding a function on

jQuery.find

Overriding functions on this level have different meaning for this. When you're working on .fn. - the this is the returned query result, which are "mixed in" to any query result.

Here, the this will be the root object itself, and the usage by it is different. These are functions or utility functions that are invoked "statically".

like image 111
Radagast the Brown Avatar answered Jul 28 '26 00:07

Radagast the Brown



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!