gsamaras
gsamaras

Reputation: 73424

Prevent enter from entering textarea

I want to allow the user to press enter and save the text of the textarea, but I want the newline NOT to get applied in the text of the textarea. Is that possible?

Here is my jsFiddle.

e.stopPropagation();

just seems to be not enough!

Upvotes: 1

Views: 913

Answers (4)

Andre.IDK
Andre.IDK

Reputation: 2047

I just tried this code here in JSFiddle and it works fine for me

$('textarea').keypress(function(event) {
   if (event.keyCode == 10 || event.keyCode == 13) {
     event.preventDefault();
   }
});

Upvotes: 0

AiApaec
AiApaec

Reputation: 660

Try this:

$('#cagetextbox').keypress(function(e){return CancelNewLine(e);});

function CancelNewLine(e)
{
    var keynum;
    if(window.event) // IE
        keynum = e.keyCode;
    else if(e.which) // Netscape/Firefox/Opera
        keynum = e.which; 

    return (keynum != 13);
}

Upvotes: 0

ankhzet
ankhzet

Reputation: 2568

You'we missed return false;.

Try here.

Upvotes: 1

sahbeewah
sahbeewah

Reputation: 2690

What you're after is e.preventDefault(). Stop propagation just stops bubbling up the DOM tree.

Upvotes: 1

Related Questions