views:

145

answers:

1

What the best way to simulate the user pressing "enter"? $(element).keypress() doesn't seem to allow me to pass in the actual key that was pressed.

+3  A: 

Demo Here

e = jQuery.Event("keypress")
e.which = 13 //choose the one you want
$("#theInputToTest").trigger(e)
redsquare
Thanks redsquare. This solution wasn't obvious to me from the docs -- http://api.jquery.com/trigger/. They show they usage as .trigger( eventType, extraParameters ). Should I have been able to figure that out based on the docs? Is this an officially supported feature.
morgancodes
@morgancodes, I guess not easily no, it is not explained but hinted at lower down.
redsquare