Skip to content Skip to sidebar Skip to footer

Textarea Does Not Stop Making New Line When Enter Key Is Pressed

i don't understand why my textarea won't stop making new lines and won't call function when enter is pressed depsite the fact jquery is to do so. With input it's working ok. And ye

Solution 1:

To stop newlines (along with carriage return), you need to capture 10 as well as 13 on keypress using keycode.

See this snippet:

$("textarea").on("keypress", function(e) {
    if ((e.keyCode == 10 || e.keyCode == 13)) {
        e.preventDefault();
        chat();
    }
});

functionchat() {
    alert("hello chat");
}
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><textareaname='mesaj'rows='7'col='60'></textarea><br/><br/><inputtype='submit'value='Trimite mesaj!'onclick='chat()' />

Solution 2:

You can do this check my code and sample example for your reference

$(".Post_Description_Text").keydown(function(e){
		if (e.keyCode == 13)
		{
		e.preventDefault();
      	}
	});
.Post_Description_Text{ 
    width:400px;
    height:100px;
}
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script><textareaname="comment_text"id="comment_text"class="Post_Description_Text"rows="5"></textarea><buttonid="check_btn">click here to check</button>

Solution 3:

Try this:

HTML(add id to your textarea):

<formid='com'method='post'>
    Mesaj:<br><textareaname='mesaj'id="mesaj"rows='7'col='60'></textarea><br/><br/><inputtype='submit'value='Trimite mesaj!'onclick='chat()'/></form>

JS(avoid new lines when enter key is pressed):

$('#mesaj').keydown(function(e) {
    if(e.which == 13) {
        e.preventDefault();
        chat();
    }
});

JSFIDDLE: http://jsfiddle.net/ghorg12110/p3ufLjoe/

Post a Comment for "Textarea Does Not Stop Making New Line When Enter Key Is Pressed"