Where jQuery stores event handlers

    In the "Internet" I did not find a detailed answer to this question. As a rule, it is recommended to use the standard method to get handlers:

    But in my case, he didn’t want to work, so I had to understand the jQuery code a bit and find where they were stored.

    So, let's start in order.

    1. All data on events in jQuery is stored in the jQuery.cache variable.
    If we look there, we will see that it is a simple array of objects: The
    numbers of elements in the array are the id's of specific objects to which this data relates.
    So how do you know which id, for example, from a document object?

    2. The id of the jQuery object is stored inside the object in a special variable, the name of which is generated when jQuery is initialized.
    And it is stored in jQuery.expando. Thus, the id of the object can be found as follows:
    elem[ jQuery.expando ]

    3. Now we know that the data for the object is stored in jQuery.cache, and we know how to get the id of the object. Get the jQuery data for our object:
    jQuery.cache[ elem[ jQuery.expando ] ]

    To receive events, you need to refer to the events of the object received above:
    jQuery.cache[ elem[ jQuery.expando ] ].events

    4. Next, you can get an array with objects where event handlers are stored, for example for 'keydown', as follows:
    jQuery.cache[ elem[ jQuery.expando ] ].events['keydown']

    5. We get an array of objects, in each of which there is a handler method - this is our event handlers:

    As an example, we list the installed handlers on the 'keydown' for the document object:
    var events = jQuery.cache[ document[ jQuery.expando ] ].events['keydown'];
    for(var i=0; i


    Also popular now: