Move event handler using jQuery

I have a problem that drives me crazy and, in extreme cases, I ask a question here.

I want to move the onclick event from an element to an onfocus event of the same element, and I use the following code:

$("#theElement").bind("focus", {}, $("#theElement").data("events").click);
$("#theElement").unbind("click");

Perhaps you guessed it. THIS DOES NOT WORK.

How do I do this job? I get the message "fn.apply is not a function" in the following code snippet from jQuery 1.3.2:

proxy: function( fn, proxy ){
        proxy = proxy || function(){ return fn.apply(this, arguments); };
        // Set the guid of unique handler to the same of original handler, so it can be removed
        proxy.guid = fn.guid = fn.guid || proxy.guid || this.guid++;
        // So proxy can be declared as an argument
        return proxy;
    }

EDIT: Sorry, I should have mentioned. This comes from a plugin that does some things when you click an item. I want to do the same thing when an element receives focus not when it is clicked, so the simplest solution I thought was because I can’t change the code for the plugin.

: . , @sje397 ,

$('#theElement').data('events').click[0]

$('#theElement').data('events').click[3]

- , , 3 ( jquery).

+3
3

, ,

$('#theElement').click(function myHandler() {
  //...
});

$("#theElement").bind("focus", {}, myHandler);
$("#theElement").unbind("click");

, .

, :

// assuming yours is the first handler
var myHandler = $('#theElement').data('events').click[0]; 

$("#theElement").bind("focus", {}, myHandler);
$("#theElement").unbind("click");

, jQuery 1.4.3+ __events__. . .

+4

, jQuery .data(), - , :

$(el).data().events.click[0].handler

, , .

+1

I do not 100% understand what exactly you want to do. Do you want to associate the focus and click event with the same element?

$("#theElement").bind("focus click", function(){
    //do stuff
    $(this).unbind('focus click');
});

edit : based on your clarification - you want to fake the 'click' event in focus ...

$("#theElement").bind("focus", function(){
    $(this).trigger('click');
});

http://api.jquery.com/trigger/

+1
source

Source: https://habr.com/ru/post/1777928/


All Articles