views:

321

answers:

2

Is there a way to focus on a text field on page load (on the iphone)? I tried:

OnLoad="document.form_id.form_field_id.focus();" on the iPhone Simulator but it didnt work.

Does anybody now why this doesnt work and if so is there a work around?

A: 

Try

OnLoad="document.getElementById('form_field_id').focus()"
Ian Kemp
I already try that with out any luck..Thanks for the effort though
A: 

It's better to use standard ways to access forms and form controls:

document.forms.form_id.elements.form_field_id.focus();

(notice form and elements addition).

or:

document.forms['form_id'].elements['form_field_id'].focus();

if form/form control ids/names have characters that can't be part of Javascript identifiers, e.g. "|", "-", ".", etc.

Short notation (as in your example), even though widely supported, is not really standardized, and it's usually better to stick to standard practices.

I'm not sure what the problem is here. Perhaps mobile Safari doesn't support programmatic focus setting? Have you tried invoking focus with a slight delay?

window.setTimeout(function(){
  document.forms['form_id'].elements['form_field_id'].focus();
}, 100);
kangax