Solved

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

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
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…

895 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now