View Full Version : <input type=text... want Enter to call a function

03-19-2003, 08:03 PM
I'm using onChange and it works fine. But I can't figure out how to also have the user's typing Enter after changing the text also call a function.

This has got to be an easy one but I'm stumped. Looked in my book and even at the search <input> box for THIS website! It uses type=hidden if that makes a difference.

03-19-2003, 08:37 PM

,just add your function to the onsubmit:

onSubmit="myfunc(); return false">

03-19-2003, 09:05 PM
This is my code and the function setSearchText is called fine by changing the text and then clicking anywhere on the page (onChange). But changing the text and then hitting the Enter key is not doing anything (onSubmit).

c+="<input type=text name=search value='"+searchText+"' onChange='setSearchText(this)' onSubmit='setSearchText(this); return false' size=10 maxlength=25 border=1>";

I'm so ignorant that I stumble on the littlest things... which is why I love this forum.


03-19-2003, 10:09 PM
I always see the onSubmit event called in the <form> I'm not sure if you can call the event from a text box.

<form onSubmit='setSearchText(this); return false'>
<input ...>

That's something to try at least.

Roy Sinclair
03-19-2003, 10:11 PM
onsubmit isn't an event for an input tag. You should use the onkey, onkeydown or onkeyup events instead. Then you could also get rid of the onchange event code since any changes would already be handled.

03-19-2003, 10:46 PM
arnyinc, it's already inside a <form> but I tried your code anyway and it didn't help.

Roy, it's a search function and you wouldn't want the function to be called until all the text was entered. I have a "search" button beside it but would also like it to work just like the search feature at this forum where you can EITHER click on GO or just hit Enter.

Roy Sinclair
03-19-2003, 11:03 PM
You still need to use one of the onkey events to do what you want, you just have to examine the key the user entered and only trigger your function when it's the enter key.

Essentially you want to do the opposite of the function requested in this thread http://www.codingforums.com/showthread.php?s=&threadid=7492.

The hooks you need to use are the same so this code would take little altering to do what you want.

03-19-2003, 11:16 PM
function checkEnter(e) {
var kC = window.event ? event.keyCode :
e && e.keyCode ? e.keyCode :
e && e.which ? e.which : null;
if (kC) return kC == 13;
else return false;


03-19-2003, 11:42 PM
You don't have to do any of that ... providing that your form has a real submit button:

<input type="submit">


<input type="image">

then pressing enter from an input box will always submit the form, and trigger the function called in <form onsubmit.

03-20-2003, 12:02 AM
Roy, I tried that and got the Enter key working easily. Thanks.

Now I have a different problem. I am trying to call the function Search() which works well from a button, but gives the error message "object expected" from my keyCheck() function. Is there a problem calling one function from another?

// Function to check for Enter key
function checkKey(){

c+="<input type=text name=search value='"+searchText+"' onChange='setSearchText(this)' onKeyDown='checkKey()' size=10 maxlength=25 border=1>";

cheesebagpipe and brothercake... I'm trying to us the Enter key to call the Search() function so you don't have to click on the Search button.

Roy Sinclair
03-20-2003, 03:51 PM
function checkKey(){

I assume the "Search" function calls the setSearchText function. Just make sure you've got the proper object reference to the input field of your form since that's how it's called in the onchange event call.