2

I would like to create new events (using document.createEvent() or Jquery.Event + copying all important attributes) and send the clone to avoid modifying the original event.
Here is the source code http://jsfiddle.net/uymYJ/20/

This question is related to this one How to avoid to modify the event object in this situation

Any ideas why when I type a key on my keyboard I get for newEvent.keyCode undefined value?

$("#box").keydown(function(event){

    var newEvent = $.Event("keydown");

    console.log(event.keyCode); // 68 
    console.log(newEvent.keyCode); // undefined
});
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
js999
  • 2,063
  • 4
  • 26
  • 34

2 Answers2

2

From the jQuery API manual:

As of jQuery 1.6, you can also pass an object to jQuery.Event() and its properties will be set on the newly created Event object.

So you could try var newEvent = $.Event('keydown', event), but as far as I know that will only create a shallow copy of the original event object.

Alternatively just copy the properties you need:

var newEvent = $.Event("keydown", {
    keyCode:  event.keyCode
});

Unless you do that, your event object will have no useful properties on it at all.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
1

http://jsfiddle.net/uymYJ/21/

var e = $.Event('keydown', en);

this does the effect you (really?) want ;)

MrFischer
  • 117
  • 1
  • 4