0

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

like:

  if(event.keyCode == 13){
    $("#submit").click();
  } 

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.

Suraj
  • 932
  • 5
  • 23
  • 43
  • 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.. – Suraj 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

2 Answers2

3

You can simulate a keypress with this code:

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

thanks to this thread: Is it possible to simulate key press events programmatically?

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.

Community
  • 1
  • 1
JMax
  • 26,109
  • 12
  • 69
  • 88
1

Check the below if it helps

<body onload="$('#submit').click();"></body>
Ghyath Serhal
  • 7,466
  • 6
  • 44
  • 60
  • 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. – Suraj Jul 21 '11 at 15:01