Phil John
Phil John

Reputation: 1237

Force picker view to pop up using Javascript on iPhone

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?

Upvotes: 0

Views: 720

Answers (2)

Phil John
Phil John

Reputation: 1237

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

Display DropDown options on Focus

Upvotes: 1

Igor Zinov&#39;yev
Igor Zinov&#39;yev

Reputation: 3706

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.
});

Upvotes: 0

Related Questions