Flaashing
Flaashing

Reputation: 771

jQuery keypress submit only on enter

when a user presses enter(submits) a comment on my site it submits, as i want it to. but if the user wants to make a new line ex the user presses shift+enter to make a new line and NOT submit

how is that made ? this is my code:

$('#commentfield').keyup(function(e){
  switch (e.keyCode) {
    case 13: //enter
$('#commentform').submit();
    break;        
   }
});

should i just make a new case with: case 13+16: ??

Upvotes: 1

Views: 1708

Answers (2)

Cory Danielson
Cory Danielson

Reputation: 14501

...or the simple solution

http://jsfiddle.net/CoryDanielson/McH8q/

jQuery/Javascript (jQuery 1.7.1)

$('textarea').on('keydown', function(event) {
    if (event.keyCode == 13) //if enter is pressed
        if (!event.shiftKey) $('#testForm').submit(); //and shift IS NOT held down, submit form
});

HTML

<form id="testForm">
  <textarea placeholder="Try Shift + Enter"></textarea>
</form>

Upvotes: 1

Sameera Thilakasiri
Sameera Thilakasiri

Reputation: 9508

Find this Fiddle http://jsfiddle.net/jishnuap/zYEMv/ this may help you.

Upvotes: 6

Related Questions