adding event on keypress in javascript 1

adding event on keypress in javascript

const elem = document.getElementById("input");

elem.addEventListener("keypress", (event)=> {
    if (event.keyCode === 13) { // key code of the keybord key
      event.preventDefault();
	 // your code to Run
    }
  });

Here is what the above code is Doing:
1. We are getting the element with the id input.
2. We are adding an event listener to the element.
3. We are checking if the key pressed is the enter key.
4. If it is, we are preventing the default action of the enter key.
5. We are running our code.

Similar Posts