views:

137

answers:

7

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.

+2  A: 

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;
    }
}
Quintin Robinson
+1  A: 

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.

Anton
it is my accepted answer, still i did not need those other stuff, just keyCode.
YourComputerHelpZ
+2  A: 

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;
}
Darin Dimitrov
+1  A: 

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.

AcousticBoom
A: 

Using keyCode was it.

It worked!

Thanks to all those who said so. I've given all those an up vote.

YourComputerHelpZ
Don't forget to mark the accepted answer as well
AcousticBoom
A: 

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.

wambotron
A: 

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.

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