I am trying to ask the user for confirmation before a radio button is selected.
Currently I'm handling the radio button's onclick event, asking the user to confirm their action and if they hit the 'cancel' button call the preventDefault( ) method for the event to cancel it.
It cancels the event if a selection has already been made for the radio button group (it actually selects it, the reverts it back to it's original value when the preventDefault( ) method is called) The problem I'm having is that the radio button is selected regardless of stopping the event if no previous selection has been made. When a radio button is not part of a group of radio buttons this radio button is always selected.
If you take the following code and past it into the code pane in the w3c schools try-it utility (click hit the 'Edit and Click Me' button) you'll get a good idea of what I"m trying to explain (Please note the following code does not work in IE)
Currently I'm handling the radio button's onclick event, asking the user to confirm their action and if they hit the 'cancel' button call the preventDefault( ) method for the event to cancel it.
It cancels the event if a selection has already been made for the radio button group (it actually selects it, the reverts it back to it's original value when the preventDefault( ) method is called) The problem I'm having is that the radio button is selected regardless of stopping the event if no previous selection has been made. When a radio button is not part of a group of radio buttons this radio button is always selected.
If you take the following code and past it into the code pane in the w3c schools try-it utility (click hit the 'Edit and Click Me' button) you'll get a good idea of what I"m trying to explain (Please note the following code does not work in IE)
Code:
<html>
<head>
<script type="text/javascript">
function doClick(e){
if(confirm("are you sure?")==false){
e.preventDefault();
e.stopPropagation();
return false;
}
return true;
}
</script>
</head>
<body>
<form>
<input type="radio" name="radio1" onclick="doClick(event);" />x
<input type="radio" name="radio1" onclick="doClick(event);" />y
<input type="radio" name="radio1" onclick="doClick(event);" />z
</form>
</body>
</html>
Comment