0

I need a text input field to be ready to type into as soon as the page opens. I've found a couple of questions like this answered, but none of them seem to be specific to whether or not it's possible to do on an iPad.

I'm using this javascript:

window.onload = function(){
    var text_input = document.getElementById('barcode');
    text_input.focus ();
    text_input.select ();  
}  

and my HTML for the input is this:

<input type="text" name="text" class="textinput" id="barcode">

When I test the webpage in Google Chrome it works, but when I test it on my iPad it doesn't. I've also tried the HTML5 autofocus element, but I've heard that doesn't work on iPad either.

MadSkunk
  • 3,309
  • 2
  • 32
  • 49
Grooms
  • 25
  • 3
  • 1
    Try removing `text_input.select ();` – gdoron Mar 04 '13 at 19:07
  • And I'm unsure, but does the space between the method and the parentheses make any difference to whether it works or not? – David Thomas Mar 04 '13 at 19:08
  • @David Thomas No it doesn't. – Matt Cain Mar 04 '13 at 19:08
  • @gdoron: I removed `text_input.select ();` and it still didn't work – Grooms Mar 04 '13 at 19:11
  • are you sure affecting the onload event from javascript is actually working under safari? I mean, if you put an alert out there does it popup? – Sebas Mar 04 '13 at 20:38
  • try this instead of your whole block: `setTimeout(function() { barcode.focus(); }, 200);` – Sebas Mar 04 '13 at 20:39
  • @Sebas: Yes, if I put an alert in there, it will popup. Also, I tried your `setTimeout` code and that didn't work either :( – Grooms Mar 04 '13 at 21:13
  • weird. Are you sure you don't declare twice the id="barcode" in your page? That's the only thing I can think of ... – Sebas Mar 04 '13 at 21:16
  • @Sebas: Yep, it's only used once. I've tried every other code I can find about having this text input be selected automatically, and nothing has worked for the iPad. I'm starting to think it's not possible. – Grooms Mar 04 '13 at 21:45

1 Answers1

0

I've had a similar issue in the past on an iPad.

You could try running your code on a button press and see if that works, if it does then it may be the same issue I had whereby the iPad wasn't allowing code that ran automatically to perform certain functions.

In my case I wanted to start an html 5 video tag playing, but could only do so on a button press and not automatically when the page loaded.

As other have suggested, try putting an alert() in your window.onload method, or try setting a timeout and running this code after a few milliseconds. If you can verify that your code is actually being run, that would be a start.

MadSkunk
  • 3,309
  • 2
  • 32
  • 49
  • After lots more research, I finally found this: [link](http://stackoverflow.com/questions/6287478/mobile-safari-autofocus-text-field). It turns out that IOS doesn't allow automatic focus as it would normally bring up the keyboard automatically, which would get annoying for users. – Grooms Mar 05 '13 at 17:55