0

When you touch on a menu item in a web page on the iPhone, a picker view appears that allows you to select from the values that are in the menu.

I have a web page with a menu, but I'm trying to get the picker view to appear programmatically rather than have the user touch it first. This is my form

<form id="myForm">
<select id="mySelect">
<option value="a">a</option>
<option value="b">b</option>
<option value="c" selected>c</option>
<option value="d">d</option>
<option value="e">e</option>
</select>
</form>

I've tried the click() function on "mySelect" and I've tried adding focus first as below

$(document).ready(function() {
$('#mySelect').focus(function() {
$('#mySelect').click();
});
  });

but nothing seems to work. I've tried this in Safari without success either, so it probably not webkit-specific.

What am I doing wrong?

Phil John
  • 1,225
  • 1
  • 15
  • 24

2 Answers2

1

It looks like this can't be done. See Rex M's answer to this similar question.

Display DropDown options on Focus

Community
  • 1
  • 1
Phil John
  • 1,225
  • 1
  • 15
  • 24
0

What you're doing here is binding an event handler to a focus event, which invokes a click method on the select field. I'm not sure if that's what you want, I would suggest you try doing it this way:

$(document).ready(function(){
    var select = $('#mySelect'); // Cache DOM requests. In this case it might be pointless
                                 // but in other it might help.

    select.trigger('click'); // The trigger method not only triggers event handlers,
                             // but also sends a native DOM event.
});
Igor Zinov'yev
  • 3,676
  • 1
  • 33
  • 49
  • 1
    I tried this, but no luck. The menu just sits there waiting for the user to click it. I guess part of my problem is that I don't know what event triggers the appearance of the menu items, let alone whether we can emulate that event in javascript. – Phil John Jun 17 '11 at 12:40
  • Oh, ok. Maybe it's the `$(document).ready` part that's causing trouble? Have you tried triggering a click right away, without waiting for the DOM to load? – Igor Zinov'yev Jun 22 '11 at 12:07