Thursday, March 21, 2013

JS/JQ simulate Enter event

THX to the awesome Eran Hertz our programming Architect!

thing is that the trigger event needs some extra stuff to trigger the right thing:

var press = jQuery.Event("keypress");
press.altGraphKey = false;
press.altKey = false;
press.bubbles = true;
press.cancelBubble = false;
press.cancelable = true;
press.charCode = 13;
press.clipboardData = undefined;
press.ctrlKey = false;
press.currentTarget = $("#myTextBox")[0];
press.defaultPrevented = false;
press.detail = 0;
press.eventPhase = 2;
press.keyCode = 13;
press.keyIdentifier = "";
press.keyLocation = 0;
press.layerX = 0;
press.layerY = 0;
press.metaKey = false;
press.pageX = 0;
press.pageY = 0;
press.returnValue = true;
press.shiftKey = false;
press.srcElement = $("#myTextBox")[0];
press.target = $("#myTextBox")[0];
press.type = "keypress";
press.view = Window;
press.which = 13;
$("#myTextBox").trigger(press);



and for the short version:
var press = jQuery.Event("keypress");
press.bubbles = true;
press.cancelable = true;
press.charCode = 13;
press.currentTarget = $("#myTextBox")[0];
press.eventPhase = 2;
press.keyCode = 13;
press.returnValue = true;
press.srcElement = $("#myTextBox")[0];
press.target = $("#myTextBox")[0];
press.type = "keypress";
press.view = Window;
press.which = 13;

p.s. - for all of u guys that came from stackoverlow.com u can grant me great joy by marking my answers, thx!!!

No comments:

Post a Comment