Problem using alert in events in jQuery Mobile on iPhone/iPad
The checkbox in the jQueryMobile html below uses an onClick event to display an alert() popup.
Clicking the checkbox puts a mark in it, and displays the alert. All ok, so far.
However, the event handler is also fired when one thereafter "leaves" the checkbox (clicking outside it, to loose focus)
Thus, the alert is displayed then as well. This is wrong.
Also, the checkbox is un-marked. This is really really wrong! :-)
I get this behaviour ONLY on iPhone/iPad (I'm testing with Safari). It works ok on Android and "computer" browsers (well, actually I haven't tried using Safari on other plattforms maybe this is a Safari issue?)
@cathal, well making sure that "checked" is set is of course a method to avoid the alert when leaving focus on the field. On the other hand, it really shouldn't fire any onClick at all - how to avoid this in jQuery?
However the real problem is not the "double onClick" but rather the fact that the checkbox is cleared when leaving focus.
That's a real pain!
the right event for checkbox is NOT onclick but onchange
Gary
Is that your whole real code above? If not is there a link to the page
There is no reason a 'lost focus'/'some other click' action could fire unless it had been written
Stefan Lennerbrant
ASKER
@leakim971, using onChange results in the exact same behaviour: an extra event when leaving (or what ever happens) the field - and also the checkbox is cleared when clicking outside the field (leaving focus)
Separate the label from the input and see if it still happens, you may be getting a double click event misfiring because you are technically clicking the label and the input
leakim971
@leakim971, using onChange results in the exact same behaviour: an extra event when leaving (or what ever happens) the field - and also the checkbox is cleared when clicking outside the field (leaving focus)
Did you really tried my whole code, just copy/pasting without any update of your?
Stefan Lennerbrant
ASKER
Sorry @leakim971 and @Cathal, I've been away for a few days.
I really appreciate your input.
However, the problem still persists. The jsfiddle setup also results in incorrect behaviour.
Could you please check out this link: http://www.tolva.se/test/ioscheck.htm
It contains links to my code (three versions, using diffrerent means to trigger the event; onclick, onchange and your suggested jQuery change)
All these results in incorrect behaviour on IOS (tested on iPhone and iPad). This is REALLY annoying :-(
Also the page above contains a link to your jsfiddle. It DOES give the same error!
If you click the checkbox to set a mark, and then click "outside" the checkbox (in the same jsfiddle window region) then the "click" is repated, unchecking the box and triggering the event.
the onchange version work fine for me on my iPhone
Stefan Lennerbrant
ASKER
@leakim971, what ios/browser do you use? I've tried with iPhone4 and iPad3
I really get the error all the time on these.
Also, I get it both with Safari and Chrome (tested in iPad)
Do you mean that you CAN repeat the problem with the onclick version, but it works ok for you with the onchange version? Or did you only test the onchange version?
Stefan Lennerbrant
ASKER
A small addition, is that the "frame" around the checkbox+text is "darkened" a little when I click to check the checkbox.
This darkening does not happen on Android or desktop computers.
Do you get such a darkened background on the checkbox field?
And again; do you get it to work on all four versions of the test?
iPhone 3
the alert isa native JS function, every browser depending the OS do what he want and as it is native you CAN'T change it but we are off topic now.
Stefan Lennerbrant
ASKER
That's true, but triggering an "extra click", changing the "checked" status on the field - that's plain wrong :-)
It obvisouly has something with the alert(), or confirm(), to do - but I'd say that there must be possible to display an alert/confirm in the event handled for a checkbox onClick/onChange, right?
So the issue is -- what the heck is the problem with the jQueryMobile/IOS combination, when using alert/confirm() in the onClick event handler? :-)
leakim971
I think you've a cde somewhere doing this extra click, I can't understand why I did not get it on my phone and you get it
if you really have this with the onchange demo, it's not about javascript but about your phone, just try it on someone else phone
I'd say it's clear that IOS devices do have a problem with alert() within onclick/onchange event handlers, as described here.
Note that the behaviour is a little instable, sometimes one may "get it to work ok" once or twice, depending on how one clicks, but eventuelly one always gets into a situation when the error displays.
Yielding to the platform with setTimeout() in the event handler however seems to solve the problem.
Of course this is a little cumbersome when arguments are to be passed to the event handler, one may need to build a framework with global variables etc, but still it is possible to work around the IOS bug.
So, I'd say that setTimeout is a good enough solution to this question.
Open in new window