Reputation: 131
I am having a textarea
in which when you press Enter alone a jquery ajax get fired up but i want to add the feature which is when you press Enter + Shift to only go to new line and not fire up the ajax. Can you guide me please with that ?
here is the code that i've written so far in jquery but when i click Shift+Enter the ajax gets fired as well, i need to eliminate that
$('.inbox_message_textarea').keydown(function(e) {
var code = e.keyCode || e.which;
if(code == 13) { //Enter keycode
console.log("You pressed Enter Key alone");
}
if( code == 13 && e.shiftKey ) {
e.preventDefault();
console.log("You pressed SHIFT+ENTER");
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea class="inbox_message_textarea"></textarea>
Upvotes: 0
Views: 41
Reputation: 21489
First check Shift+Enter press and then check Enter press. Also use else if
instead of second if
$('.inbox_message_textarea').keydown(function(e) {
var code = e.keyCode || e.which;
if (code == 13 && e.shiftKey) {
e.preventDefault();
console.log("You pressed SHIFT+ENTER");
} else if (code == 13) {
console.log("You pressed Enter Key alone");
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea class="inbox_message_textarea"></textarea>
Upvotes: 1