How to control pressing ENTER to trigger the onBlur () event?

I have an iPad webapp with a big <form> at some point. Each input in it has functions that control values, both for keyUp events and for Blur.

The fact is that if the user accidentally pressed the "GO" button during input (considered by pressing "enter"), the form is submitted. I would like to intercept this aspect and trigger the onBlur () event of the focused element.

At the moment I have this:

 load(){ document.addEventListener("keydown",logPressedKeys,false); } /** * logs the keys hit in the console, will eventually trigger my onBlur event */ function logPressedKeys(e) { console.log(e.keyCode); if (e.keyCode==13) { console.log('Enter spotted: prevent!'); e.preventDefault();//Shall prevent submitting return false;//Hoping it prevents default if above fails } } 

Do you have any tips / ideas / improvements?

Nota bene: There must be at least one input focused on the iPad keyboard to pop out.

+4
javascript ipad
source share
2 answers

Found document.activeElement working in iPad Safari.

 function logPressedKeys(e) { console.log(e.keyCode); if (e.keyCode==13) { e.preventDefault(); console.log('Enter spotted: prevent!'); temp=document.activeElement; //console.log(temp); temp.onblur(); return false; } return true; } 
+2
source share

This will not work in every browser.

document.addEventListener ("KeyDown", logPressedKeys, false);

Use this:

  document.onkeydown = function (e) { alert(e.keyCode); } 
0
source share

All Articles