Take the 2-minute tour ×

I am trying to press tab and enter key through javascript/jquery on page load.


  if(event.keyCode == 13){

this would only work when user presses the enter key but I want the javascript to do this stuff for the user on page load.

share|improve this question
you want to simulate a keypress for your code to catch it ? did i miss something or can't you find anything simplier ? –  JMax Jul 21 '11 at 14:59
Ya, I want to simulate a keypress through javascript.. –  tunetosuraj Jul 21 '11 at 15:00
This looks really wrong, I am 99% certain there is a better way to achieve what you want. Perhaps if you explained why you need to press tab and enter on page load you'd get more helpful answers. –  Ollie Edwards Jul 21 '11 at 15:06
add comment

2 Answers

up vote 3 down vote accepted

You can simulate a keypress with this code:

function simulateKeyPress(character) {
  jQuery.event.trigger({ type : 'keypress', which : character.charCodeAt(0) });

thanks to this thread: Simulate JavaScript Key Events

but as Ghyath Serhal pointed out, there might be a better way than to simulate a keypress to then catch it by your same code.

share|improve this answer
add comment

Check the below if it helps

<body onload="$('#submit').click();"></body>
share|improve this answer
No, I don't want to submit a form like this. It won't work. First it needs focus, so I am trying to simulate tab key and then enter key. –  tunetosuraj Jul 21 '11 at 15:01
add comment

Your Answer


