By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,083 Members | 1,608 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,083 IT Pros & Developers. It's quick & easy.

Shift+Enter doing different function than just Enter from textarea

eboyjr14
P: 27
I need to make it so that when you're entering data into a textarea, and press shift+enter it creates a new line where the caret is at, then when you press enter it sends a message. This is like most chat applications.

What's wrong with this code:

Expand|Select|Wrap|Line Numbers
  1. <textarea id="msginput" onkeypress="return onEnter(event,this.value,this);" onkeydown="chkShift(event);"></textarea>
and...

Expand|Select|Wrap|Line Numbers
  1. function chkShift( evt )
  2.     {
  3.     var key = null;
  4.     if( evt.which )
  5.         {
  6.         key = evt.which;
  7.         }
  8.     else if( evt.keyCode )
  9.         {
  10.         key = evt.keyCode;
  11.         }
  12.     if ( key == 16 )
  13.         {
  14.         document.getElementById('shifter').value = "true"
  15.         }
  16.     else
  17.         {
  18.         document.getElementById('shifter').value = "false";
  19.         }
  20.     }
  21. function onEnter( evt, msg, obj )
  22.     {
  23.     var keyCode = null;
  24.     if( evt.which )
  25.         {
  26.         keyCode = evt.which;
  27.         }
  28.     else if( evt.keyCode )
  29.         {
  30.         keyCode = evt.keyCode;
  31.         }
  32.     if( 13 == keyCode )
  33.         {
  34.         if (document.getElementById('shifter').value == "true")
  35.             {
  36.             obj.value += "\n";
  37.             document.getElementById('shifter').value = "false";
  38.             }
  39.         else
  40.             {
  41.             if (msg != "" && document.getElementById('titletext').innerHTML != "" )
  42.                 {
  43.                 sndMessage(msg, document.getElementById('titletext').innerHTML);
  44.                 obj.value = "";
  45.                 }
  46.             }
  47.             document.getElementById('shifter').value = "false";
  48.         return false;
  49.         }
  50.     return true;
  51.     }
Jun 1 '07 #1
Share this Question
Share on Google+
1 Reply


acoder
Expert Mod 15k+
P: 16,027
Use event.shiftKey to detect the shift key.
Jun 1 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.