Capture "done" button click in iPhone's virtual keyboard with JavaScript
I was unable to track the 'done' button being clicked. It didn't register any click
s or keypress
es. I had to addEventListener
s for change
, focusout
and blur
using jquery
(because the project already was using jquery
).
You need to do some kind of this:
$('someElem').focusout(function(e) {
alert("Done key Pressed!!!!")
});
It worked for me, hope it will help you as well.
After searching and trying this solution basically is say:
document.addEventListener('focusout', e => {});
tested on IPhone 6s
This question is kinda old, but I've found a hacky way recently to make this working.
The problem with the 'blur', 'focusout' events is that they fire even if user just tapped outside the input/textarea, and did not press the 'Done' button, in my case, UI should behave differently depending on what exactly have happened.
So to implement it, I've done the next thing:
- After showing the keyboard (the input received the focus), add
click
handler on thewindow
via theaddEventListener
function. When user clicks on the window, remember the timestamp of the click in the variable (let's call itlastClick = Date.now()
) - In the
blur
event handler, set a timeout for 10-20 ms to allow other events happening. Then, after the timeout, check if theblur
event happened in a time difference lower for example than 50-100 ms than thelastClick
(basicallyDate.now() - lastClick < 50
). If yes, then consider it as a 'Done' button click and do corresponding logic. Otherwise, this is a regular 'blur' event.
The key here is that tapping on keyboard controls (including Done button) does not trigger the click
event on the window
. And the only other way to make keyboard hide is basically tap on other element of the page and make the textarea lose focus. So by checking when the event happened, we can estimate whether that's a done button click or just blur event.