...

View Full Version : Enter Key to Trigger Custom Button



jason_kelly
10-11-2011, 09:25 PM
Hello,

I need your help.

I basically need, when the enter button is pressed on the keyboard to trigger a button. Currently, my code works but it just refreshes the page and does not actually trigger the desired button:



//HERES MY CUSTOM BUTTON:

<BUTTON TYPE="submit"
onMouseOver="goLite(this.name)"
onMouseOut="goDim(this.name)"
onclick="javascript:v7_search()"
CLASS="btn_std"
NAME="btn_search"
>&nbsp;? Search

</BUTTON>



Here's the code to make the enter button work:



<form name="frmIMTS"
onkeydown="javascript:if (event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('btn_search').click();}};"
>


Any help with this is greatly appreciated.

Cheers,

J

Old Pedant
10-12-2011, 03:03 AM
Yes, it really does trigger the button.

But because the button is type="submit" the normal action of the button is, indeed, to submit the <form> containing it to the server. Which then quite naturally reloads the HTML page, as it is supposed to.

If you don't want this to be a submit button, then don't make it one.

If all you really are trying to do is invoke the function specified by the onclick, then do that, instead of invoking click on the function.

jason_kelly
10-12-2011, 08:19 PM
Thanks Old Pendant.

A little progress here, however, I still cant get the button to fire when the user hits enter

here is my code:



<html>

<head>

<SCRIPT TYPE="text/javascript">

function test(){

alert("testing123")

}

</script>


</head>

<body>

<form name="frmIMTS"
onkeydown="javascript:if (event.which || event.keyCode){if ((event.which == 13) || (event.keyCode == 13)) {document.getElementById('btn_search').click();}};"
>
<BUTTON
onclick="javascript:test()"
NAME="btn_search"
>PRESSME
</BUTTON>
</form>


</body>

</html>


A hopeless soul needs your help.

Much thanks and appreciation in advance.

J



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum