On iOS 7, <select>
menus in Mobile Safari do not fire a change event until after the user clicks "Done". Are there any other events I can listen for to know when the user's selection has changed?
<select onchange="alert('This will not fire until the user clicks Done')">
<option>1</option>
<option>2</option>
</select>
There is no way to determine the new selection value before the user touches "Done".
When the user first touches the <select>
control, iOS opens a native UI for the select options. This is not DOM. And when the user touches "Done" it sends the new value/index and fires the corresponding event back on DOM.
You can test this. Just setup two events: onfocus
will setup a timer and continuously poll for selectedIndex
until onblur
destroys the timer when "Done" is touched.
<select id="my-select" onfocus="checkSelectedIndex()" onblur="selectDone()">
<option>1</option>
<option>2</option>
</select>
var timer;
// timer setup on focus
function checkSelectedIndex() {
timer = window.setInterval(function () {
console.log('selected index:', document.getElementById('my-select').selectedIndex);
}, 500); // 2 polls per second
}
// timer cleared on blur
function selectDone() {
console.log('Selection changed!');
if (!timer) { return; }
window.clearInterval(timer);
}
With this test, you'll see that even though you change the selected item on the UI; the logged index will not change until after you touch "Done" (which is when the final message is sent to DOM).
So, what you can do is; (with some UX trade off); instead of a <select>
element, create a custom control which pops up a list of options in a <div>
or <ul>
. This way, you'll NOT be triggering the native UI and the user will not leave the DOM. And you'll be able to listen to the events of the menu and clicked items.
Just make sure your control feels mobile-friendly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With