?
Solved

Submitting form on enter but placing line break on shift enter.

Posted on 2006-06-30
5
Medium Priority
?
614 Views
Last Modified: 2007-11-27
Hello,

I'm trying to compile a quick script to perform a similar action to what is present in many chat programs today.  What I would like to do is allow the person to press 'Enter' to submit the form and post the data but when the use is holding 'shift' down I want the enter key to function as normal.

I'm sure with enough trial and error I can work it out for myself, but I'm sure someone has already been there, done that.  Copied below is a function which would perform the reverse action and always submit the form on enter but just need to have 'shift' clause added into it.  The code also needs to be a multi-browser as possible.

<script type="text/javascript">          
function ifEnter(field,event) {
var theCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
if (theCode == 13){
document.forms[0].submit();
return false;
}
else
return true;
}      
</script>

Thanks all.
Anubis.
0
Comment
Question by:Anubis2005
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
5 Comments
 
LVL 2

Expert Comment

by:sandylaw
ID: 17022093
I believe the keycode for shift+enter is:  #13
0
 
LVL 1

Author Comment

by:Anubis2005
ID: 17022134
Hello,

I have actually found another source which will be able to produce this result.  SCript is copied below for PAQ.

Thanks
Anubis.


<HEAD>

<SCRIPT LANGUAGE="JavaScript1.2">

<!--

function NNKeyCap(thisOne)
{
    if (thisOne.modifiers & Event.SHIFT_MASK)
{
    if (thisOne.which == 37)
       {alert('That\'s the % key')};
    if (thisOne.which == 90)
       {alert('That\'s the Z key')};
    if (thisOne.which == 41)
       {alert('That\'s the ) key')};
}

    if (thisOne.which == 61)
       {alert('That\'s the = key')};
    if (thisOne.which == 106)
       {alert('That\'s the j key')};
    if (thisOne.which == 51)
       {alert('That\'s the 3 key')};
}

function IEKeyCap()
{
if (window.event.shiftKey)
{
    if (window.event.keyCode == 37)
       {alert('That\'s the % key')};
    if (window.event.keyCode == 90)
       {alert('That\'s the Z key')};
    if (window.event.keyCode == 41)
       {alert('That\'s the ) key')};
}

    if (window.event.keyCode == 61)
       {alert('That\'s the = key')};
    if (window.event.keyCode == 106)
       {alert('That\'s the j key')};
    if (window.event.keyCode == 51)
       {alert('That\'s the 3 key')};
}

if (navigator.appName == 'Netscape') {
window.captureEvents(Event.KEYPRESS);
window.onKeyPress = NNKeyCap;
}

//-->

</SCRIPT>

</HEAD>

<BODY onKeyPress="IEKeyCap()">
0
 
LVL 2

Expert Comment

by:sandylaw
ID: 17027112
Looks like Anubis2005 found her own solution, so no objections on my part.
0
 
LVL 1

Accepted Solution

by:
GhostMod earned 0 total points
ID: 17047268
Closed, 250 points refunded.

GhostMod
Community Support Moderator
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question