How to do that when clicking on a key a submit is sent [duplicated]

1

Well it's the following, I have a chat on my site, the chat has a text field and a send button that is to send. In other words, the person types in the text and then has to click the send.

I'd like to know how you would do that by clicking enter, submit was sent. That is, you do not have to click on the submit button to send, but just hit enter to send the message.

Submit code:

 <button class="btn btn-primary" type="submit" data-loading-text="<i class='fa fa-spinner fa-spin'></i> WAIT..." id="send_massage" style="margin-right:6px;">SEND</button>
    
asked by anonymous 05.12.2015 / 00:32

3 answers

4

You can do this: ( source )

$("#id_of_textbox").keyup(function(event){
    if(event.keyCode == 13){
        $("#id_of_button").click();
    }
});
    
05.12.2015 / 00:40
1

You can actually send the form by any key.

See this example in the JSFIDDLE that I put together. You can enter any key code.

The codes can be seen here , just enter the specific key on the form and the code appears next to it.

CODE

$('input[name="texto_exemplo"]').keypress(function (e) {
  if (e.which === 32) { /* 32 é a tecla de espaço, use 13 para [ENTER]*/
    if($('form[name="exemplo"]').submit()){
      alert("Formulário enviado!");
    }
    return false;
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script><formaction="" name="exemplo" method="POST">
  <input type="text" accesskey="s" name="texto_exemplo"/>
</form>
    
05.12.2015 / 00:56
0

Well, with javascript you can use the onkeypress () event to perform if the tight button was the enter, if you call submit , would look like this:

  <input type="text" id="txtSearch" onkeypress="return searchKeyPress(event);" />
<input type="button" id="btnSearch" Value="Search" onclick="doSomething();" />

<script>
function searchKeyPress(e)
{
    // look for window.event in case event isn't passed in
    e = e || window.event;
    if (e.keyCode == 13)
    {
        document.getElementById('btnSearch').click();
        return false;
    }
    return true;
}

function doSomething(){
  alert('submited');
}
</script>

Example on JSFiddle.

Now, if you want to use jQuery, just use the keyup () event to do the same check, like this: / p>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script><inputtype="text" id="txtSearch"  />
    <input type="button" id="btnSearch" Value="Search"/>


<script>
$("#txtSearch").keyup(function(event){
    if(event.keyCode == 13){
        $("#btnSearch").click();
    }
});
  
$('#btnSearch').click(function(){
  alert('test');
});
</script>

Example in JSFiddle.

Source: SOen

    
05.12.2015 / 00:47