Enter key no input event

2

Good morning, I have a input out of a form and I would like it when someone typed it and hit the enter key with some function, but I do not know how to do it.

<input type="text" value="texto" />
    
asked by anonymous 27.01.2017 / 13:18

2 answers

4

You can do 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" />
    
27.01.2017 / 13:21
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><inputid="textbox" type="text" value="texto" />
    
27.01.2017 / 13:28