0

I have an input using a onkeypress trigger, and I'm trying to find a way to fire the event trigger with a specific keycode. I've looked around but all I find are jQuery solutions, and I don't want to use jQuery.

For a click trigger you'd just use document.forms[0].elements[1].click() but there doesn't seem to be a keypress() equivalent for onkeypress.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Marvin
  • 11
  • 2
  • Possible duplicate of http://stackoverflow.com/questions/2490825/how-to-trigger-event-in-javascript – Selvakumar Arumugam Jan 03 '12 at 22:52
  • 1
    Please learn to use google: http://www.google.ca/search?q=javascript+onkeypress+example yields answers. First entire page don't refer to jquery. http://www.w3schools.com/jsref/event_onkeypress.asp – Ahmed Masud Jan 03 '12 at 22:52
  • 1
    @AhmedMasud Most of those results only help with binding to an event, not with triggering it programmatically. – Jonathan Lonowski Jan 03 '12 at 23:00
  • Ahmed, please learn to read. My question says "I'm trying to find a way to fire the event." – Marvin Jan 03 '12 at 23:35
  • SKS, that solution doesn't work for me I'm afraid since I don't know how to obtain the event object. That example has you _create_ an event object for the entire document. I'd have to know how to _retrieve_ an event object from an event made with a regular `` first. – Marvin Jan 03 '12 at 23:39
  • @Marvin: It sounds more like you want to eavesdrop on the events being triggered rather than create and trigger your own events. – bennedich Jan 04 '12 at 00:17
  • @bennedich, I'm not sure I follow. What do you mean? – Marvin Jan 04 '12 at 00:20
  • @Marvin: I mean, do you want to programmatically trigger a keypress event to invoke the handler `doSomething`, or do you want to catch/eavesdrop on `event` when something else triggers `doSomething`? – bennedich Jan 04 '12 at 00:35
  • @bennedich, I want to trigger the keypress event, of course. That's what I have been saying all along. – Marvin Jan 04 '12 at 00:51
  • @Marvin: I think you are mixing up words. An event triggers the keypress event handler. If you want to trigger the event handler programmatically you must create a custom event and dispatch it, just as described in SKS link. In your answer to SKS you made it sound like you wanted to intercept events, triggered elsewhere, before they invoke the handler. – bennedich Jan 04 '12 at 00:58
  • @bennedich, So there is no way get `keypress()`-type behavior like there is for `click()`? Ouch. That's a pain. And my answer to SKS was just saying I lack a precondition to use the solution SKS linked - I don't know how to get an event object for a pre-existing `onkeypress="return doSomething(event)"`. PS: You don't need to do @Marvin when I am the poster of the question you are commenting to. – Marvin Jan 04 '12 at 01:09
  • Take a harder look at what SKS linked. The only precondition required is the possibility to include Javascript. – bennedich Jan 04 '12 at 01:32

1 Answers1

-1

You can create an event and dispatch it to your Object. just like this:

if(document.createEvent) { 
  var evObj = document.createEvent('KeyEvent'); 
  evObj.initEvent( 'keypress', true, false); 
  evObj.keyCode = 13; // Set your keyCode
  document.getElementById("myid").dispatchEvent(evObj); 
}

under IE/FF:

var evObj = document.createEventObject();
evnObj.keyCode = 13;
document.getElementById("myid").fireEvent("onclick",evObj);
event.cancelBubble = true;
ijse
  • 2,998
  • 1
  • 19
  • 25
  • Getting `Error: NOT_SUPPORTED_ERR: DOM Exception 9` on Chrome 16.0.912.63 (Official Build 113337); testing another browser. – Marvin Jan 04 '12 at 00:57
  • Getting `Error: Operation is not supported` on Firefox Mozilla/5.0 (Windows NT 5.1; rv:7.0.1) Gecko/20100101 Firefox/7.0.1; testing Opera. – Marvin Jan 04 '12 at 00:59
  • Getting `Unhandled DOMException: NOT_SUPPORTED_ERR` on Opera 11.60 Build 1185; testing Internet Explorer. – Marvin Jan 04 '12 at 01:03
  • Wait, this computer has IE6 for legacy testing. I don't have a newer version available atm. Sorry. I'm afraid `evObj = document.createEvent('KeyEvent');` won't work. – Marvin Jan 04 '12 at 01:05