Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Disable Enter Key, except for text areas. 1

Status
Not open for further replies.

checkai

Programmer
Jan 17, 2003
1,629
US
Is there a way to disable the action of the enter button except when the focus is within certain text boxes?

"...your mom goes to college..."
 
The only thing I can think of is to put an onkeypress event handler on the body tag to call a function, then inside that function check to see if the enter key was pressed. If so, it checks to see if focus is in one of the allowable textareas. If that is the case, do what you need to (submit, other function call, etc.):

This would be the script function call:


Code:
<body onkeypress="checkKeyCode()">

Here would be an example of a script:

Code:
<script type="text/javascript">
   function checkKeyCode() {
      //keyCode 13 = enter key
      // if enter key was pressed
      if (event.keyCode == 13) {
         //here check to see if focus is currently in the textarea(s)
         if (document.getElementById("textAreaId").focus() {
            //do something
         }
      }
   }

</script>





<.

 
This code actually set focus to my text box and put a return in it.

Code:
  function checkKeyCode(){
    //keycode 13 = Enter Key
    if (event.keyCode == 13) {
      if (document.getElementById('<%=txtAddress.UniqueID %>').focus()){
        return true;
      } else {
        return false;
      }      
    }
  }

"...your mom goes to college..."
 
Hmm I'm sorry, I learn something new everyday, I could of sworn that .focus() returned a value.

Anyway, I'll think of a way to get around that.

I apologize for giving you code that didn't work.

[evil]

<.

 
I was actually thinking the same thing as what that said, except for the onclick part, which would be handled by the onkeypress event handler.

Try this:

Code:
<script type="text/javascript">
[!]var safeToEnter = 0;[/!]
   function checkKeyCode(e) {
      //keyCode 13 = enter key
      // if enter key was pressed
      if (e.keyCode == 13 [!] && safeToEnter[/!]) {
         //do something if focus in textarea
      }
      else {
         //do something if focus NOT in textarea
      }
   }

</script>
Then on the textarea tags you will allow an enter key:

Code:
<textarea cols="12" rows="12" onfocus="safeToEnter=1" onblur="safeToEnter=0">
blah blah
</textarea>

Your body tag should read like this:

Code:
<body onkeypress="checkKeyCode(event)>

That will make it work in FF as well as IE

* Note: I did test this and it did work






<.

 
Thanks, I got the same thing to work finally!!

"...your mom goes to college..."
 
Nice work monksnake... and congrats on the TipMaster of the Week award too :)

[sub]Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
[/sub]

Enable Apps
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top