See this jsFiddle:
http://jsfiddle.net/nathanfriend/vAcpc/9/
It seems that the preventDefault() method doesn’t prevent the clicked radio button from toggling (notice that the message always says “This radio button was checked”, regardless of its prior state). However, after the onclick() method finishes, the radio buttons snap back to their initial states (except for the very first time a radio button is selected).
It seems like preventDefault() works not by actually preventing the radio button from being checked, but rather by just returning the set of buttons to their prior state. Can anyone explain this behavior? I’d like to be able to completely prevent the radio button from ever being toggled – this way I could accurately check to see if the radio button was checked inside its onclick() method. Any ideas?
Actually a
mousedownevent will get fired first and check your radio button. You can easily verify this by holding the mouse button on a radio button. However, almost all GUI elements only get executed or changed if bothmousedownandmouseupare fired and successfully run.See this example (demo):
As you can see the
mousedowngets fired first, and it will alert ‘This radio button was not checked in mousedown’. Theclickevent is prevented because the actuallymouseupevent gets canceled by the nasty alert. However, if you useconsole.logyou’ll notice thatclickis still executed and the radio button is still checked virtually.However, if you check for active radio buttons you’ll notice that none is active. The behavior of
clickelement is sometimes irritating, bute.preventDefault()will do it’s job, don’t worry.