views:

123

answers:

2

Hi, I have a form which I want the user to fill in and I want to have the keypad to pop up on the iphone automatically when they hit a JSP page instead of them tapping on the input box first. What is the special tag in order to achieve that? (note this is not an objective C app)

Also, is it possible to set a timer, eg: 3 seconds, then have the keypad pop up, instead of popping up immediately.

Thanks in advance.

+1  A: 

It should be done automatically for an HTML page input field (on entering it)... thus calling YourFormElement.focus(); or YourFormElement.select(); should work.

In particular you can take advantage of HTML5 and specify things like:

<input type="number"/> <!-- opens the keyboard in number mode -->

this doesn't break anything either since browsers that don't understand "number" as a type, fallback to "text" as the default.

Note: to set this up there are various possibilities (but here's a quick example):

HTML:

<input id="yourIDgoesHere"/>

Vanilla JavaScript (after the field has rendered):

<script type="text/javascript">
  document.getElementById('yourIDgoesHere').focus();
</script>

Using jQuery:

<script type="text/javascript">
  $(document).ready(function(){
    $('#yourIDgoesHere').focus();
  });
</script>
scunliffe
sorry, I missed the bit about auto-focusing the element to bring the keyboard up...
scunliffe
Thanks for the quick reply.where do I call YourFormElement.focus(); or YourFormElement.select();?sorry I am pretty new in this.
Linda
works for webbrowser, it auto selects as the cursor is flashing, but does not work for iPhone safari browser =S :(
Linda
@Linda - you're right (I hadn't noticed) it looks like Safari on the iPhone (and iPad) restricts the use of .focus() for this... :-(
scunliffe
hmm, whats the reason for .focus() and .select() being restricted? are their any alternate solutions? :) thx
Linda
I'm guessing they don't want to "confuse" users by having the keyboard popup... esp. right after a page navigation... e.g. if you typed in the address bar "Google.com" and pressed [Go] it would almost immediately hide, then show the keyboard... and users might not understand that they went to Google, and it auto-opened the keyboard for searching. I'm not aware of any alternatives at the moment, but I run a bunch of tests later today to see if there are any viable options.
scunliffe
A: 

I don't think it's possible to do without having the user tap the box first, however you can use Javascript to select the box for them, which should open the keypad for the user.

i.e. onload:

document.formname.element.focus()
Peeps
<body onload="document.myform2.mytextfield2.focus();"><form name="myform2"><input type="number" pattern="[0-9]*" name="mytextfield2"></form></body>on a normal webbrowser, the text field is autoselected as the it is flashing a cursor, however it does not work when I try it on the iPhone. Why??? thanks
Linda