1

Hello this is what i have in the head of my file:

function entsub(event)
 {
 if (event && event.which == 13) 
 write1();
 else
 return true;
 }

and in the body, my form is:

<form id="writeform" name="writeform">
 Message: <br />
 <textarea name="message" rows="3" id="message" style="width:90%;" onkeypress="return entsub(event)"></textarea>
 <br />
 <input name="send" type="button" id="send" value="Send" onclick="write1()" />
 <span id="sent"></span>
 &nbsp;&nbsp;&nbsp; <input type="reset" value="Reset" />
 </form>

I need to make it work that when i press enter when i am in the textarea, it does not make a new line but send it [see my <input type="button">]

But it won't work! It keeps making a new line... Using IE8 now.

asked Oct 26, 2009 at 18:33
0

6 Answers 6

2

Well i'm not sure what write1() does, but it would be prudent to return false when the condition is met..

function entsub(event)
{
 if (event && event.keyCode == 13) {
 write1();
 return false; //Return false to prevent default execution
 //Some browsers use event.preventDefault() etc.
 }else{
 return true;
 }
}
answered Oct 26, 2009 at 18:37
Sign up to request clarification or add additional context in comments.

Comments

2

Usually the cross browser test is the following:

function entsub(e) {
 if ((e.which && e.which == 13) || (e.keyCode && e.keyCode == 13)) {
 write1();
 } 
 return true;
}
answered Oct 26, 2009 at 18:44

Comments

2

If you want to use jquery, you can do this:

 $(document).ready(function() {
 $("#txtTest").keypress(function(e) {
 var code = (e.keyCode ? e.keyCode : e.which);
 if (code == 13) {
 doSomething();
 return false;
 }
 });
 });
 function doSomething() {
 alert("I did it!");
 }

where txtTest is the id of your textarea.

answered Oct 26, 2009 at 18:49

Comments

1

Use keyCode instead of which.

Depending on the browser, you need to prevent the default action. Check out event.cancelBubble and event.returnValue for IE and event.stopPropagation() and event.preventDefault() for Firefox.

answered Oct 26, 2009 at 18:38

1 Comment

it is my accepted answer, still i did not need those other stuff, just keyCode.
0

For a non-js related way, I'm pretty sure you can just use an input of type="submit" and it will use enter to submit the form. At that point, you would just move your event to onsubmit.

answered Oct 26, 2009 at 19:00

Comments

0

A cleaner way is to change to <input type="button"> to <input type="submit"> and move the onclick="..." to a onsubmit="..." on the form. This would not require so much error-prone JavaScript and would work in more cases.

answered Oct 26, 2009 at 19:05

1 Comment

the problem is that if i would do that, it wont send it. Do not know why, got this code from some guy.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.