Event key enter into input

Asked

Viewed 15,546 times

4

Good morning, I have one input out of a form and I would like when someone typed in it and hit enter some function run, but I don’t know how to do..

<input type="text" value="texto" />
  • Related: http://stackoverflow.com/questions/8803376/form-submit-on-keycode-enter-13

2 answers

9


You can do it like this:

const inputEle = document.getElementById('enter');
inputEle.addEventListener('keyup', function(e){
  var key = e.which || e.keyCode;
  if (key == 13) { // codigo da tecla enter
    // colocas aqui a tua função a rodar
    alert('carregou enter o valor digitado foi: ' +this.value);
  }
});
<input id="enter" type="text" value="texto" />

2

You can use the jQuery function Keypress

jQuery('#textbox').keypress(function(event){

	var keycode = (event.keyCode ? event.keyCode : event.which);
	if(keycode == '13'){
		alert('You pressed a "enter" key in textbox');
	}

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="textbox" type="text" value="texto" />

Browser other questions tagged

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