retrieving current input field

P

please-answer-here

Scenario:

I'm using the following code to avoid that users use the return key in the
middle of a form to submit the entire form
---- snip ---------
function kH(e) {
var pK = document.all? window.event.keyCode:e.which;
return pK != 13;
}

document.onkeypress = kH;
if (document.layers) document.captureEvents(Event.KEYPRESS);
---- snip ---------

What I would do is to modify the code so that when the users is in a
textarea input field they are allowed to use the return key. Is there a
funcion/method I can use in the function to test in which named input field
is the active one?
 
A

askMe

Curious. Why aren't you using an onsubmit event? Or are you? I don't
understand why your form is getting submitted when someone presses the
return key. Is this a self processing form?

http://askblax.com
 
P

please-answer-here

askMe said:
Curious. Why aren't you using an onsubmit event? Or are you? I
don't understand why your form is getting submitted when someone
presses the return key. Is this a self processing form?

I don't know neither. The code is inherited. Here is a stripped version:
---[snip]---

<%
response.write("<html><head>")
response.write("<title>Testing</title>")
%>

<script type="text/javascript">
function kH(e) {
var pK = window.event.keyCode;
return pK != 13;
}
document.onkeypress = kH;
</script>

<%
response.write("</head>")
response.write("<body>")
response.write("<form action=""showmodel.asp"" name=""dummy"" >")
response.write("<br><input name=""a"" type=""text"" autocomplete=""off"" >")
response.write("<br><input name=""b"" type=""text"" autocomplete=""off"" >")
response.write("<p><input name=""c"" type=""submit"" >")
response.write("</form>")
response.write("</body>")
response.write("</html>")
%>

Without the onkeypress handler the form gets submitted when i press return
in any of the inupt fields
 
S

Sandfordc

Here I have a simple fix for you,

<script>
function check(cde){
if(cde == 13){
event.returnValue=false
}
}
</script>

<body onkeydown="check(event.keyCode)">

This will do the trick, but remember it will TOTALLY disable the enter
key like previously sugessted maybe try,

<form onsubmit="check(event.keyCode)">

Then you have to submit the form by clicking the submit button.

Hope this helped.

Best Regards,
Sandfordc
http://www.javascript-central.tk
 
P

please-answer-here

Sandfordc said:
Here I have a simple fix for you,

<script>
function check(cde){
if(cde == 13){
event.returnValue=false
}
}
</script>

<body onkeydown="check(event.keyCode)">

This will do the trick, but remember it will TOTALLY disable the enter
key like previously sugessted maybe try,

Hey
Thanks for the advice.
BUT - I'm not having any problems in disabling the return key. Works fine
with my own code. What I would like to implement is that I want to modify
this code, so that when a user is in a textarea they are allowed to use the
return key. So my question is:

can I modify my code or check() above so I can check in which (input) field
the cursor is placed when the key is pressed?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top