...">

Press enter to enter the form to lose focus.

I have a form with an input field and a hidden submit field:

<form action="javascript:void(0);">
    <input type="text">
    <input type="submit" style="display:none;">
</form>

      

I would like to make it so that when the enter button is pressed, the input field just loses focus.

How can i do this?

+3


source to share


2 answers


Try it. Note that you need to include a jquery file for this.



<form action="javascript:void(0);">
    <input type="text" id="txtFocus">
    <input type="submit" style="display:none;" id="btnHidden">
</form>
<script>
$("#btnHidden").on('click', function() {
        $('#txtFocus').blur();
});
</script>

      

+3


source


Give your input id for convenience and you can do it with this little function using simple javascript



<form action="javascript:void(0);">
    <input id="input1" type="text">
    <input type="submit" style="display:none;">
</form>
<script>
document.getElementById('input1').addEventListener('keyup',function(e){
    if (e.which == 13) this.blur();
});
</script>

      

+4


source







All Articles