Unfocus an input of a form after pressing enter

Viewed 3039

I have an input field and I want to disable automatic submit and unfocus the field after pressing Enter.

Code

<input type="search" id="keyword" placeholder="Search..." name="keyword"
       onkeydown="if(event.keyCode==13){return false; $('#keyword').blur();}"
       autocomplete="off" />

The form does not get submitted nor unfocused.

Thanks in advance!

1 Answers

You are returning from function before blur is triggered. You need to call blur first, and then return from function. Please see corrected snippet:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="search" id="keyword" placeholder="Search..." name="keyword" onkeydown="if(event.keyCode==13){ $('#keyword').blur(); return false;}" autocomplete="off">

Related