I can not understand where I prescribe keydown while doing so but does not work

 $(document).ready(function() { $(window).keydown(function(e) { if (e.which == 13) { var message = $("#message").val(); var idVal = $("#id-val").val(); $.ajax({ type: 'GET', url: "send.php", data: {message: message, idVal: idVal}, cache: false, success: function(html){ $('#message').val(''); } }); } }); }); 
But when you click everything works if you do so

 $(document).ready(function() { $("#clickButtonForm").bind("click", function show() { var message = $("#message").val(); var idVal = $("#id-val").val(); $.ajax({ type: 'GET', url: "send.php", data: {message: message, idVal: idVal}, cache: false, success: function(html){ $('#message').val(''); } }); }); }); 
html looks like this

  <form class="input-group" style="margin-top: 5px;" action="" > <input type="text" name="message" value="" id="message" class="form-control teo-two" placeholder="Search for..."> <input type="hidden" name="id-val" id="id-val" value="1,2"> <span class="input-group-btn"> <button class="btn btn-secondary" id="clickButtonForm" type="button">Go!</button> </span> </form> 

How to make everything work when you click and when you press enter?

  • And where should the focus be when you press Enter for your magic to work? In #message ? Maybe it's easier for you then to process the submit event? - br3t
  • almost but not quite - Vadim

2 answers 2

Added by

 function submit_handler(form) { return false; } 

and in the form <form class="input-group" style="margin-top: 5px;" action="" onsubmit="return submit_handler(this)" > <form class="input-group" style="margin-top: 5px;" action="" onsubmit="return submit_handler(this)" > now submit submit event

     window.onkeydown = function(e) { if (e.which == 13) { alert(e.which); } }; 

    • so does not fit tried + how the click will work - Vadim