Forums

submit form AND execute JS function when "enter" is clicked

marklandry 21 Aug, 2012
Hi, I have the following that I'd like to submit when the enter key is clicked,
However, it needs to also run a function.

here's what I have:

<form>
<input type="textbox" onkeypress="if(event.keyCode==13) {codeAddress();}"/>
<input type="Submit" value="Submit" onclick="codeAddress()"/>
</form>


But it doesn't work. The form submits but the function (codeAddress()) doesn't fire (unless you hit "submit" then it works fine).

Thanx for any help you can provide.

Mark
GreyHead 22 Aug, 2012
Hi Mark,

This appears to work in a Load JS action:
window.addEvent('domready', function(){
  $('textbox').addEvent('keyup', function(event){
    if ( event.key == 'enter' ) {
      event.stop();
      codeAddress();
    }
  });
});
function codeAddress() {
  alert('codeAddress');
}
marklandry 23 Aug, 2012
Works great - thanx Bob.
This topic is locked and no more replies can be posted.