Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

force safari iOS select component to update when options change

When you tap on a select input on a web page using iOS (iPhone), a spinner widget (the "picker") pops up and lets you spin through and select options within that select. Let's say you've tapped into one of these and the selector widget is open. While this is open, if you use javascript to modify the select options (add, remove, update options via the dom), then these changes don't get reflected in the widget unless the user closes and reopens the widget.

Is there a way to force the browser to update the options automatically?

enter image description here

Edit: Here is an example you can use to observe how updating select options doesn't update the selector widget: http://jsfiddle.net/RrsNk/

<select id="my-select" />

$(function () {
    updateSelect();
});

function updateSelect() {
    $("#my-select").empty();
    for (i = 0; i < 5; i++) {
        var ran = Math.random();
        $("<option />").attr("value", ran).html(ran).appendTo("#my-select");
    }
    setTimeout(updateSelect, 2000);
}
like image 201
Rafe Avatar asked Aug 20 '13 15:08

Rafe


2 Answers

Safari uses a UIPickerView to display the dropdown menus. As you would expect, the title of the dropdown component in the page is updated according to the changes in the DOM but the picker view is not tightly coupled with the DOM so it isn't updated. The situation is the same with Chrome and Opera Mini.

So in conclusion, it is not possible what you are trying to implement. You should look for other ways to make your dataset accessible.

like image 184
allprog Avatar answered Sep 22 '22 11:09

allprog


I was brought to this question as the closest to my usage case, which is similar but not identical, and might apply to other people as it is a common situation. I found the answers here a touch confusing, so just wanted to clarify my findings.

  1. I start with a dropdown containing just one option: Searching....
  2. When the user clicks, the "picker" pops up (a "tick-list" on iPad, same principle as the "spinner" on iPhone), showing Searching....
  3. An AJAX call gets come choices from the server, and JavaScript updates the options[] to these.
  4. At this point, the picker still shows just Searching..., and the user has no clue that it has been repopulated. The user must click outside to dismiss the "picker", and click the dropdown again to see the new choices.

So in my case I do not have OP's situation of wanting a second dropdown to be displayed depending on what option is selected in the first dropdown. Rather I would like (#1) the "picker" disappear, and then (#2) reappear (with the new choices).

Now, it seems you can do #1, but not #2.

  1. You can dismiss the "picker" by dropdown.blur() (tested in Mobile Safari). See Close a SELECT dropdown list programatically with Javascript/jQuery.
  2. You cannot cause the "picker" to pop up again (at least not in Mobile Safari). See How can you programmatically tell an HTML SELECT to drop down (for example, due to mouseover)?.

So I am at least doing #1, so that the user gets some feedback rather than just being stuck with Searching....

If anyone can state how you can do #2 on Mobile Safari I would be very interested to hear....

like image 38
JonBrave Avatar answered Sep 21 '22 11:09

JonBrave