How can I disable the ENTER key on my textarea?
Asked Answered
C

4

25
<form name='qform'>
<textarea name='q' rows='3' cols='60' wrap='hard' id='q' onkeydown="if (event.keyCode == 13) document.getElementById('clickit').click()"></textarea>
<input type='button' value='search' id='clickit' onclick="get();">
</form>

I have this form... it doesn't have a submit button because I am using jquery and under this form is a div area where the results will be shown. It is a search engine that does not have an input box but instead has a textarea. This is because it will be a multiple word searcher.

The problem is that if I press enter, the query is submitted and everything is ok ... but the focus on textarea goes down one line and that is a problem for me.

Basically I want the enter to have that one function only(submit) end nothing else.

Cesarcesare answered 15/7, 2011 at 23:31 Comment(2)
You could just put the focus back on the textarea after the submit. But if the user clicks the "search" button, won't focus go to the button anyway?Turnsole
Does this answer your question? Disable New Line in Textarea when Pressed ENTEREvonneevonymus
S
13

In the jquery function, use event.preventdefault and next do what you like. For example

<script>
$("a").click(function(event) {
  event.preventDefault();
//Do your logic here
});
</script>

http://api.jquery.com/event.preventDefault/

Scent answered 15/7, 2011 at 23:39 Comment(0)
O
23
$(document).ready(function() {

    $('textarea').keypress(function(event) {

        if (event.keyCode == 13) {
            event.preventDefault();
        }
    });
});
Oro answered 21/4, 2014 at 16:9 Comment(1)
keyCode is deprecated. For standards compatibility, use event.key === "Enter" instead.Goldin
E
15

Why not just use <input type="text"> if you don't want multiple lines? You mentioned it will be a "multiple word searcher". Why does this require a <textarea>?

Update

Try this

$('textarea').bind('keypress', function(e) {
  if ((e.keyCode || e.which) == 13) {
    $(this).parents('form').submit();
    return false;
  }
});
Equip answered 15/7, 2011 at 23:33 Comment(4)
You might want to change the return false; to e.preventDefault(), just in case there are keypress bindings further up the DOMDonegal
Assuming you don't want those bindings, yes :)Equip
REASON: I am sending text messages, they cannot have line breaks, yet at the same time I don't want the display of the text box to scroll to the right -- thus a textarea is appropriate.Blynn
@Blynn Assuming you're talking about SMS, linebreaks would normally be allowed.Equip
S
13

In the jquery function, use event.preventdefault and next do what you like. For example

<script>
$("a").click(function(event) {
  event.preventDefault();
//Do your logic here
});
</script>

http://api.jquery.com/event.preventDefault/

Scent answered 15/7, 2011 at 23:39 Comment(0)
F
0

Pure javascript:

document.addEventListener('keypress', function (e) {
    if (e.keyCode === 13 || e.which === 13) {
        e.preventDefault();
        return false;
    }

})
Firecure answered 11/10, 2022 at 11:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.