Click to See Complete Forum and Search --> : Enter Key Detection on Input Fields


jce_step
03-18-2003, 07:42 PM
Hi im john from Portugal,

Im trying to use a INPUT TYPE TEXT field to read input data from users. I want that any user after inputing the data on the field, woud only have to press ENTER button on keyboard to make the data be called by a function inside the JS code. I don't want to use INPUT TYPE BUTTON or OnKeyup function to do the same. I tried to do this with FORMS, here is a sample of my code:
----------------------------------------------------------------
<script>
var all, txt, btn, txtarea;

function init(){
txt = document.getElementById("txt");
btn = document.getElementById("btn");
all = document.getElementById("all");
txtarea = document.getElementById("txtarea");
}

function teste() { alert(all.txt.value); }

function mult(x){

x= x*2;
alert(x);
return txtarea.value = x;
}

</script>

<body onload="init();">
<form id="all" onSubmit='mult(all.txt.value)'>
<input type="text" id="txt" />
<br/>
<input type="button" id="btn" onclick='teste()' value="?"/>
</form>
<textarea id="txtarea"></textarea>
</body>
---------------------------------------------------------------
This code is an example of what im trying to do, this code reads the data from the input field and multiplys it by 2, after that it shows the result on alert POPUp window and TEXTAREA.
The alert popUp windows are working fine, what doesn't work is the TEXTAREA.

PLEASE HELP!

jce_step
03-19-2003, 02:11 PM
Yes, thats exactly what i'm saying! Only the textareaFields aren't working.

I've tried already with ' txtarea.value = x; ' But it doesn't work also.

Maybe im trying a wrong aproach on this subject...

What im simply trying to get is, the data from any input fields after the user press the ENTER KEY.
Any ideas ?

jce_step
03-20-2003, 05:48 PM
That is correct Dave!

khalidali63
03-20-2003, 06:25 PM
The correct syntax to reference a form element is

document.formName.elementName...

If you change your function as below

function mult(x){
x= x*2;
alert(x);
document.form1.textarea.value = x;
}

And you should not use the keywords or element tag names for their names as well.
<textarea name="tarea"

could be a better way.

Make the changes and if there is nothing else wrong it will produces the required result

Cheers

Khalid

khalidali63
03-20-2003, 06:51 PM
Originally posted by Dave Clark
......
... So that I don't have to guess at possible solutions.
...
Dave

Thats a fair assumption.

In the above code snippet

all = document.getElementById("all");

will most certainly confuse IE browsers.

As I said using keywords for any reference name is not a good idea.

Cheers

Khalid

jce_step
03-22-2003, 05:53 PM
I get no indication of error !
But Listen, try copy and paste the whole code it should function on your PC too.

What i am trying to do is simple, but i just don't know how to do it ...

I'm trying to read the user input data when he or she presses the ENTERKEY on the keyboard!

That's just what i want to do!

Maybe you could give me a diferent idea how to do this...

jce_step
03-25-2003, 06:51 PM
Tnks Dave, it seems to work for what i want ! ;)

hameed
08-31-2005, 07:55 PM
Hello,
I need to change the 'tabkey', which jumps to next field, to the 'Enterkey'. Could you please tell me how did you solve your problem with the Enterkey? Thanks.