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?
Edit: Here is an example you can use to observe how updating select options doesn't update the selector widget: /
<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);
}
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?
Edit: Here is an example you can use to observe how updating select options doesn't update the selector widget: http://jsfiddle/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);
}
Share
Improve this question
edited Aug 20, 2013 at 20:51
Rafe
asked Aug 20, 2013 at 15:25
RafeRafe
9,2959 gold badges53 silver badges69 bronze badges
2
- no, the IOS select works in the same modal-ish way that prompt() and confirm() do... – dandavis Commented Aug 28, 2013 at 20:40
- cars. does it somehow – Patrioticcow Commented Mar 16, 2017 at 21:11
4 Answers
Reset to default 3 +50Safari uses a UIPickerView
to display the dropdown menus. As you would expect, the title of the dropdown ponent 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.
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 mon situation. I found the answers here a touch confusing, so just wanted to clarify my findings.
- I start with a dropdown containing just one
option
:Searching...
. - When the user clicks, the "picker" pops up (a "tick-list" on iPad, same principle as the "spinner" on iPhone), showing
Searching...
. - An AJAX call gets e choices from the server, and JavaScript updates the
options[]
to these. - 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.
- You can dismiss the "picker" by
dropdown.blur()
(tested in Mobile Safari). See Close a SELECT dropdown list programatically with Javascript/jQuery. - 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....
Worth trying: populate a second <select>
element, and .focus()
to it when the item in the first select list is chosen.
Note that iOS only allows a control to be focused within a user interactive events (touchend or click event) so I am guessing Safari Mobile might allow .focus()
to work within a <select>
(to opening the second select list).
Might not work, since an onchange
event of a select list might not be deemed "interactive" or Safari Mobile might otherwise disallow selectElement.focus()
to work for some reason.
Edit: I think this shows it is unlikely to work: force safari iOS select ponent to update when options change
Don't bother trying if you need to contact a server to fill the list. That is because when you get the response event, you are definitely not in a user interactive tap/click event handler, so .focus()
is sure to be ignored (although there is a very slight chance an async chain could work).
Firstly, why would you want to - you should adjust the values in your HTML file, or with JavaScript as the page is loading.
However, if the values are changing as the user selects the dropdown, you can use JavaScript to select another element, then reselect the dropdown, causing it to reload.
You can bring focus to an element with the focus() method. Eg. Document.getElementById("input").focus();