1

Is there any way to show keyboard with selected edit text. I used focus() which is perfectly work for in browser. I am able to edit text when browser is ready. Below is my code

$(document).ready(function() {
    var allSelects = document.getElementsByTagName("p");
    var lastSelect = allSelects[allSelects.length - 1];
    lastSelect.focus();

    $("#main_container").click(function() {

        lastSelect.focus();

    });
});

but when I load this site in mobile device then it is not appears mobile keyboard unless touch.

please find out the solution. Thanks in advance

manan5439
  • 898
  • 9
  • 24

3 Answers3

1

I tried to create a working snippet with your code. I changed a little the syntax to use .on() and .trigger() methods.

Maybe you can use the .trigger() method with touchstart to achieve what you want:

$(document).ready(function() {
  var allSelects = document.getElementsByTagName("p");
  var lastSelect = allSelects[allSelects.length - 1];

  $("#main_container").on('click', function() {
    lastSelect.focus();
    $(lastSelect).trigger('touchstart');
  });

  $("#main_container").trigger('click');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="main_container">
  <p contenteditable=true>Some text</p>
  <p contenteditable=true>Some text</p>
  <p contenteditable=true>Some text</p>
</div>

Hope it helps.

Takit Isy
  • 9,688
  • 3
  • 23
  • 47
0

Try using:

$(document).ready(function() {
var allSelects = document.getElementsByTagName("p");
var lastSelect = allSelects[allSelects.length - 1];
lastSelect.focus();
lastSelect.select();

$("#main_container").click(function() {

    lastSelect.focus();

});

});

ikirachen
  • 77
  • 7
0

If you're using iOS/Safari as your mobile device, it doesn't allow setting focus unless it's in response to a user input event (i.e. touch).

I think this is a feature of mobile Safari rather than a bug. In our work on FastClick, my colleagues and I found that iOS will only allow focus to be triggered on other elements, from within a function, if the first function in the call stack was triggered by a non-programmatic event. In your case, the call to setTimeout starts a new call stack, and the security mechanism kicks in to prevent you from setting focus on the input.

See this post

Mike B.
  • 1,422
  • 12
  • 8