|
-
April 1st, 2009, 11:03 PM
#1
Input Box and enter
Hello ,
I have a bunch of HTML input text boxes(spread over 2 pages) and I need to do the following two things:
1) On the first page I have an input box and when the user enters the data and presses the
ENTER Key I need to go to the second page/url
2) The second thing I need to do is the second page which has many input boxes I need to be
able to enter data in a box and when the user presses the ENTER key the focus moves to the
next input box.
How can I accomplish that with JavaScript
thanks very much in advance for any suggestions
Susan
-
April 2nd, 2009, 06:20 AM
#2
Re: Input Box and enter
Why ask a JavaScript question in a Java forum? They are completely different languages.
The greatest obstacle to discovery is not ignorance, but the illusion of knowledge...
D. Boorstin
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
-
April 2nd, 2009, 07:45 AM
#3
Re: Input Box and enter
While I agree with dlorde that you should post this in the Client Side Scripting section, this is a relatively easy question.
When you hit the enter key while in a text box the "onChange" event is triggered. You can then either call a javascript function that will change the focus OR you can assign it directly in the onchange as such:
<input type="text" name="text1" onChange="this.form.text2.focus()">
<input type="text" name="text2" onChange="this.form.text1.focus()">
This silly example will switch from text 1 to text 2 back to text 1 every time you hit enter. If you haven't entered anything into the field (not a required field) use the onBlur instead of onChange (or both).
As far as your other question you can do the same thing, except call the submit button:
<input type="text" name="text1" onChange="this.form.submit()">
-
April 2nd, 2009, 02:22 PM
#4
Re: Input Box and enter
Thanks very much for the replies. I am sorry - I posted this last night and I must have misread it.
I tried the things u suggested and it works fine except that in addition to the text box I also have a Submit button so hitting Enter calls the javascript to redirect the page and also executes the Submit.
What should I do to avoid that?
Thanks
Susan
-
April 2nd, 2009, 02:41 PM
#5
Re: Input Box and enter
I think you can use something like:
Code:
<script language="javascript" type="text/JavaScript"">
document.onkeypress = keyPress;
var count = 1;
function keyPress() {
if (event.keyCode == 13 && event.srcElement.type != 'textarea' && event.srcElement.type != 'submit')
document.onkeydown false;
document.onkeydown true;
}
</script>
This should work for IE anyway.
You could also try to completely disable the enter key from ever submitting the form by:
<input type="submit" value="submit" name="saveButton" ONKEYDOWN="if (event.keyCode == 13){return false;}">
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|