Run text input

I noticed a new way to post status comments on Facebook. Basically, when you write your comment, you don’t have buttons for a click, but an enter key to click to send a comment. Now I would like to do that too. And I was thinking about possible solutions. It occurs to me that they are <input>sent with the enter button pressed, but this text field is not.

  • Or I write input (type of text) and set the upper caps of the height more than the line
  • Or I need to add a text box and somehow submit this form when the user presses the enter key.

What's better? And if the answer = second, how can I do this?

+3
source share
4 answers

, .

, , javascript

, .

, - : ( jQuery)

<textarea id="mytextarea"></textarea>
<script>
$('#mytextarea').keypress(function(e){
  if(e.keyCode == 13 && !e.shiftKey) {
   e.preventDefault();
   this.form.submit();
  }
});
</script>

, - , , . .

: http://jsfiddle.net/MEtGg/

+7

, , imo.

jquery keydown/up

0

No need to create a framework (e.g. jQuery) just for this:

var textarea = document.getElementById("area");

try {
    textarea.addEventListener("keydown", keyPress, false);
} catch (e) {
    textarea.attachEvent("onkeydown", keyPress);
}

function keyPress(e) {
    if (e.keyCode === 13) {
        alert("Enter key was pressed")
    } else {
        return;
    }
}

Take a look at an example here: http://fiddle.jshell.net/yuCAd/3/

0
source

I would go with the second. And it will use javascript to intercept the input key, and then call the function to send. This answer should do the following: Run a button click using JavaScript on the Enter key in the text box

0
source

All Articles