Solved

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

Posted on 2006-06-30
5
607 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

762 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

19 Experts available now in Live!

Get 1:1 Help Now