Hi ian,
First, you will need to determine whether or not the textarea has the focus.
To do this, you need to create a variable to indicate whether or not it has
the focus, add an event handler for the "onfocus" event, and add an event
handler for the "onblur" event. Example:
<body onkeypress="Key Check(event)">
<script type="text/javascript"><!--
var textHasFocus = false;
function KeyCheck(e)
{
var KeyID = (window.event) ? event.keyCode : e.keyCode;
if (!textHasFocus && KeyID == 13)
{
event.returnVal ue=false;
event.cancelBub ble = true;
}
}
// --></script>
<form>
<textarea rows="2" name="S1" cols="20" onfocus="textHa sFocus=true"
onblur="textHas Focus=false"></textarea>
<input type="submit" value="Submit" name="B1">
</form>
</body>
Note that I also changed your function a bit, so that it will work in all
browsers (at least Mozilla and IE). In IE, there is a window.event object,
but not in Mozilla. In Mozilla, the event is passed to the event handler
from the object that raised it. So, the function handles the event according
to the browser it is run on.
What's going on here, is that the onfocus event happens when the cursor is
placed inside the textarea. The event handler sets the "textHasFoc us"
variable to true. When the user tabs out of the textarea, or clicks
somewhere else, or types outside the textarea, the "onblur" event sets the
"textHasFoc us" variable to false. The "KeyCheck" function checks the value
of this variable, and only disallows the ENTER key when it is false.
--
HTH,
Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.
"ian" <is*******@hotm ail.com> wrote in message
news:11******** ************@g4 7g2000cwa.googl egroups.com...
Hi,
I am currently using a Javascript function to dissallow the enter key
on my ASP.NET (2.0) web page, as follows:
function fnTrapKP(){
if (document.all)
{
if (event.keyCode == 13)
{
event.returnVal ue=false;
event.cancelBub ble = true;
}
}
}
I call this from the body of my master page as follows:
<body onkeypress="fnT rapKP();">
On one of my pages I have a multiline textbox. The function above now
stops a user from untering multiple lines of text in to this textbox.
Is there any way I can modify the function to only allow the enter key
to be allowed when my multiline textbox has the focus?
Thanks
Ian