How to do that by clicking a key a Submit is sent

Asked

Viewed 1,779 times

1

Good is the following, I have a chat on my site, the chat has a text field and a send button that is to send. That is, the person type in the text and then have to click on the send.

I would like to know how to get the Submit sent when you click enter. In other words, you wouldn’t have to click on the Submit button to send, but just press enter to send the message.

Code of the Submit:

 <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>

3 answers

4


You can do it like this: (source)

$("#id_of_textbox").keyup(function(event){
    if(event.keyCode == 13){
        $("#id_of_button").click();
    }
});
  • Thank you, it worked perfectly.

  • 1

    -1, if possible add to source of that answer. After adding the font I remove the -1.

  • @So Gonçalo marks his answer as sure, bruh!!!

  • 1

    @Randrade I hadn’t seen it. Well, the same to me.

  • 1

    Font added, had forgotten to put. Good that solved your problem.

  • Now we do! @Gonçalo!

Show 1 more comment

1

In fact you can send the form by any key.

See this example on JSFIDDLE that I rode. You can put any key code.

The codes can be seen here, just type the specific key into the form and the code appears next.

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>
<form action="" name="exemplo" method="POST">
  <input type="text" accesskey="s" name="texto_exemplo"/>
</form>

  • 1

    Eduardo, your answer is good, but if you can, post at least the js code here, because if the links break, your answer is invalid.

  • 1

    Thank you, @Randrade! Good practice is good practice!!!

  • 1

    I only edited for snnipets suitability, no specific error. But you missed to mention that you are sending with space. :)

  • Thanks anyway! Well, I added in the comment code. I think we are documenting well the question hehee

  • 1

    Now yes, +1. What a pity that there is already such an answer, and it will be closed. But this does not invalidate your answer.

  • 1

    The important thing is that we document the questions and answers in the SOF in Portuguese, so our community grows and becomes a reference.

Show 1 more comment

0

Good, with javascript you can use the event onkeypress() to realize if the button pressed was enter, if it is you call the submit, would look this way:

  <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 in Jsfiddle.

Now, if you want to use jQuery, just use the event keyup() to make the same check, in this way:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<input type="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>

Exemlo no Jsfiddle.

Source: Soen

Browser other questions tagged

You are not signed in. Login or sign up in order to post.