If your code is executed via something that was initiated via a user action then it will work.
E.g;
this works (pops keyboard):
<input type="text" id='foo'><div onclick='$("#foo").focus();'>click</div>
this doesn’t work (input gets a border but no keyboard pop):
<input type="text" id='foo'>
<script>
window.onload = function() {
$("#foo").focus();
}
</script>