1

iOS has an unusual behavior when it comes to focusing elements.

You can only focus element from JS when focus is initiated from user action, such as clicking somewhere.

However, iOS will also report element as being in focus, when Safari has prevented focus and element is not in focus.

Example:

$(function() {
  $('#sad').focus();
});
#sad {
  background-color: red;
}

#sad:focus {
  background-color: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="sad">

The input is green on page load on my iPad, but there is no keyboard.

I need to know when element actually has focus. Is there any other way?

undsoft
  • 789
  • 2
  • 12
  • 21
  • Use the pseudo-selector :focus on whatever element you are trying to check for. (e.g. $("#sad:focus").length ) will tell you how many elements with the id sad are focused. – SidTheBeard Jul 18 '17 at 12:06
  • It will report it as focused, when keyboard is not shown. – undsoft Jul 18 '17 at 12:10

0 Answers0