Home > Enterprise >  How do I use the enter button in Html?
How do I use the enter button in Html?

Time:03-28

hey i'm making an chat program but I dont know how to assign the "enter" button to a onclick function.

<input id = "SendMessage" onclick="Send()" value = "Send" class = "Button" type = "submit">

thats my code. does someone have an awnser? Have a nice day!

CodePudding user response:

You need to create a keydown event listener like this:

var input = document.getElementById("input");
input.addEventListener("keydown", event => {
  if (event.keyCode === 13) { //13 is the keycode of ENTER
    Send();
  }
});

function Send(){
  alert(input.value   " - has been sent");
}
<input id = "input" type="text"/>
<input id = "SendMessage" onclick="Send()" value = "Send" class = "Button" type = "submit">

  • Related