If e.keyCode is the "enter" button and the element contains other text the condition is verified

Viewed 621

I use keyup() method on input element:

i would like to know if is possible to do that the button pressed is enter but the input field is not empty then alert a variable. I tried:

$('#search').keyup(function(e) { //#search is id input
  if (($('#search').val() != "") && (e.keyCode == 13)) {
    alert($('#search').val());   //alert
    $(this).closest('form').submit();
  }
});

Doesn't work... because the condition is true also if the input element is empty :/

This is jsfiddle if you want help me: https://jsfiddle.net/0br6q72r/

Sorry for my english and thanks a lot! :)

2 Answers
Related