I’ve got some jQuery that displays a confirm box if the user chooses a value of ‘Yes’ in a select field.
In the confirm box, if the user clicks the OK button, I want the value to remain as it is. If the user clicks the Cancel button, I want the value to change back to what it was previously.
I’ve almost got this working with the code below, except that when the user clicks the Cancel button, the value remains as ‘Yes’ instead of returning to ‘No’.
Any help in fixing this would be appreciated.
<script>
$(document).ready(function(){
$('#publish_details').change(function(){
if($(this).val() == 'Yes')
{ window.confirm("Are you sure you want to publish these details?"); }
});
});
</script>
<select name="publish_details" id="publish_details" />
<option value="No" selected="selected">No</option>
<option value="Yes">Yes</option>
</select>
Thanks,
Ste
You aren’t setting a new value if not confirmed:
Or you can simply return the confirm value (false if not confirmed):