Forum Moderators: open

Message Too Old, No Replies

press [enter] as well as clicking the [submit] button

Help needed: upon pressing the [enter] key, the submit button is selected

         

jameswsparker

6:07 am on Jan 21, 2009 (gmt 0)

10+ Year Member



This is a problem that has been pestering me for a long time now.
I have written a simple code in HTML which uses frames; basically, when you type in something in the text-box, if it

recognizes it from the script it will take you to it's set page, and if it's doesn't, then it'll take you to another set page

informing the user that it doesn't recognize your entry.
It's like a ChatBot basically.

What I need help with though is two things:
1. my first issue is when you up on pressing the [Enter], or [Return] key it does nothing, and you instead have to click the

'Submit' button; what I want is to be able to press the [Enter] key within the text-box and it automatically clicking that

button for me (like a search engine or login page would, etc)

2. And my second problem is that, when I load up my page; I want my cursor to automatically be set in the text-box, like with

Google; so that as soon as you load up the page you can type something in, and it'll appear in the text-box automatically,

without you having to click the textbox first.

If you geniuses are able to solve this for me, I literally will love you forever. Haa ha
I'm not kidding, I have been trying to figure this out for ages, so I wish you the best in cracking a method.
Anyway, I would love to hear from you soon... All the best :) And a million thanks for reading/helping. :)

jameswsparker

6:13 am on Jan 21, 2009 (gmt 0)

10+ Year Member



Oh, before I forget; this is the code for my header frame.
There are two frames, and one index to link them both together.
This is the 'header' frame, and all documents from this frame, shall be displayed in the 'main' frame.

Here's a copy of my code:

<html>
<HEAD>

<meta http-equiv="Content-Language" content="en-gb">

<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function Login(){
var done=0;
var data=document.login.data.value;
if (data=="8") { parent.main.window.location="8.html"; done=1; }
if (done==0) { alert("Could you please rephrase that?"); }
}
// End -->
</SCRIPT>

<title>Header</title>
<base target="_self">

<BODY>

<center>
<form name=login>
<table width=225 border=0 cellpadding=3 style="border-collapse: collapse" bordercolor="#111111" cellspacing="0">
<tr><td>
<p align="center"><input type=text name=data size="20"></td><td>
<p align="center"><input type=button value="Submit" onClick="Login()"></td></tr>
</table>
</form>
</center>

</html>

rocknbil

4:58 pm on Jan 21, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome aboard jameswparker, your first problem was just discussed yesterday in the Javascript forum [webmasterworld.com]. Buttons don't haven inherent behavior so you need to use a submit button and move your action to the form onSubmit().

my second problem is that, when I load up my page; I want my cursor to automatically be set in the text-box . . .

something like

<body.onload="document.forms['login'].data.focus();">

should do it, but you are best off adding ID's to your form elements and referencing it by ID. Personally I don't like this method much as the form often loads before the entire window, I may already be in the password field (or in your case, any other page area) by the time it triggers and moves focus back to the user name field, typing my password in plain text there. Annoying. [webmasterworld.com] :-)

Four other additions:

Change

<SCRIPT LANGUAGE="JavaScript">

to

<script type="text/javascript">

, the comments <!-- Begin and // End --> are no longer required, always quote form attributes, and add id's to your form objects as suggested:

<script type="text/javascript">
function Login(){
var done=0;
var data=document.getElementById('data').value;
if (data=="8") { parent.main.window.location="8.html"; done=1; }
if (done==0) { alert("Could you please rephrase that?"); }
return false;
}
</script>

<title>Header</title>
<base target="_self">

<body.onload="document.getElementById('data').focus();">

<form name="login" id="login" onSubmit="return Login();">
<table width=225 border=0 cellpadding=3 style="border-collapse: collapse" bordercolor="#111111" cellspacing="0">
<tr><td>
<p align="center"><input type="text" name="data" id="data" size="20"></td><td>
<p align="center"><input type="submit" value="Submit"></td></tr>
</table>
</form>