Answers for "press enter event in javascript"

2

javascript detect enter press on input

var el = document.getElementById("myInputID");
el.addEventListener("keydown", function(event) {
    if (event.key === "Enter") {
        // Enter key was hit
    }
});
Posted by: Guest on January-06-2020
3

hit enter button lick in js

<form>
    <input id="myInput" placeholder="Some text.." value="">
    <input type="submit" id="myBtn" value="Submit">
</form>

<script>
var input = document.getElementById("myInput");
input.addEventListener("keyup", function(event) {
    if (event.keyCode === 13) {
        event.preventDefault();
        document.getElementById("myBtn").click();
    }
});
</script>
Posted by: Guest on May-25-2020
4

javascript key pressed enter

$('.container').on('keydown', 'input', function(e) {
  if (e.keyCode === 13) {
    e.preventDefault();
  	e.stopImmediatePropagation();
    //Do your stuff...
  }
});
Posted by: Guest on June-11-2020
2

html button press enter off

Explicit Prevention:

<form>
    <label for="name">Name:</label>
  	//Not able to press `Enter`
    <input type="text" name="name" onkeypress="ExplicitPrevention(event)">
  	//Able to press `Enter`
    <input type="submit" value="Submit">
</form>

<script>
    const ExplicitPrevention = function (event) {
      	var keyPressed = event.keyCode || event.which;
      	if (keyPressed === 13) {
          	alert("Test: You pressed the Enter key!!");
        	event.preventDefault();
      	}
    }
</script>

//Also check: https://www.tjvantoll.com/2013/01/01/enter-should-submit-forms-stop-messing-with-that/
//and: https://www.geeksforgeeks.org/how-to-disable-form-submit-on-enter-button-using-jquery/
Posted by: Guest on December-17-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language