2

I'm trying to use "Enter" in a text input field to submit the message (in SignalR chat). I've tried hundreds of methods, but can't seem to get it working.

I either wanna have it so when I press enter it clicks the btn or presses tab then enter.

here is the input and btn

<input class="form-control" id="message" maxlength="200" />

<input type="button" class="btn btn-default" id="sendmessage" value="Send" />
0

2 Answers 2

2

Change

<input type="button" ...

To

<input type="submit" ...

or

<button type="submit" ...

See also button type documentation.

Sign up to request clarification or add additional context in comments.

1 Comment

@CAMELYON - make sure you wrap your form with the form element.
0

If you don't want to use form submit, you can define the logic in javascript,

document.getElementById("message")                    // to get the text box
  .addEventListener("keyup", function(evt) {          // Keyup -> Any key pressed
    if (evt.keyCode == 13) {                          // 13 for enter
      document.getElementById("sendmessage").click();
    }
  });

For keycodes, you can refer this site

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.