Skip to content Skip to sidebar Skip to footer

Simulate Pressing [enter] Key On Input Text

My code worked perfectly on changed the value of input automatic on page load with a delay between it. But there's another problem, When I tried to simulate pressing the [enter] k

Solution 1:

Submitting the form by default will submit the form and reload the page.

If you want to trigger keypress event:

var input = document.querySelector("._7uiwk._qy55y");
var ev = document.createEvent('Event');
ev.initEvent('keypress');
ev.which = ev.keyCode = 13;
input.dispatchEvent(ev);

If you want to check if pressed key is enter:

var input = document.querySelector("._7uiwk._qy55y");
input.addEventListener('keypress', function(ev){
  if(ev.keyCode === 13 || ev.which === 13){
    // enter was pressed
  }
});

If you want to prevent submitting the form on enter/submit:

var form = document.querySelector("._k3t69");

form.addEventListener('submit', function(ev){
    ev.preventDefault();
    // you can do something alternative here e.g. send AJAX request to serverreturnfalse;
});

JSFIDDLE

Post a Comment for "Simulate Pressing [enter] Key On Input Text"