0

Right now i am wondering is this below is possible:

I have some element which has some event. Let's say for now that on element click i show alert('clicked);

And the question: is there is any way to grab events of this element and add this events to for example beyboard's left arrow button ?

What i'm looking for is: grab all events from one element and attch this events to another element - in this case it's keyborad button, but this may be for example another DOM element.

Thanks for advice / strategy / info if this is possible.

Edit: i'v found this code snippet, but this don't work, but address well waht i try to do:

function copyEvents(source, destination) {
        // Get source events
        var events = source.data('events');

        // Iterate through all event types
        $.each(events, function(eventType, eventArray) {
            // Iterate through every bound handler
            $.each(eventArray, function(index, event) {
                // Take event namespaces into account
                var eventToBind = event.namespace.length > 0
                    ? (event.type + '.' + event.namespace)
                    : (event.type);

                // Bind event
                destination.bind(eventToBind, event.data, event.handler);
            });
        });
    }
    var oldE = $('.old');
    var newE = $('.new');
    copyEvents(oldE, newE);

I'm getting this error from jquery.js: jquery.2.0.js:4 Uncaught TypeError: Cannot read property 'length' of undefined

user3573535
  • 545
  • 2
  • 7
  • 29
  • As I am getting you have to find all events bound with an element by : http://stackoverflow.com/questions/2008592/can-i-find-events-bound-on-an-element-with-jquery and can bind them in just simple manner we attach click and other events to elements. You have to use the handler already bounded with that element. – Jitendra Khatri Mar 20 '17 at 13:43

1 Answers1

0

The keypress event builtin to jquery already handles this.

var xTriggered = 0;
$( "#target" ).keypress(function( event ) {
  if ( event.which == 13 ) {
     event.preventDefault();
  } else if(event.which == event.VK_LEFT){
     event.preventDefault();
     //runfunction();
  }
  xTriggered++;
  var msg = "Handler for .keypress() called " + xTriggered + " time(s).";
  $.print( msg, "html" );
  $.print( event );
});

$( "#other" ).click(function() {
  $( "#target" ).keypress();
});

then, just look for which key you want link to key event codes: http://docs.oracle.com/javase/7/docs/api/java/awt/event/KeyEvent.html

Jaynesify
  • 19
  • 4