Solved

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

Posted on 2006-06-30
5
610 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
  • 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
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…

838 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