Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

radio button change only after confirm() returns true

I've got a set of radio buttons in my html site. When the users changes the selection I show a dialog via the confirm('') function. I just want to change the selection if the user confirms the change - otherwise just cancel the event. My problem now is that I only got an event AFTER the value has been changes and the change is already done. How can i prevent this behaviour?

like image 928
soupdiver Avatar asked Oct 20 '25 02:10

soupdiver


2 Answers

$('.rdio').click(function(){
 var cnfrm = confirm('Are you sure?');
 if(cnfrm != true)
{
 return false;
}

})​

live demo : http://jsfiddle.net/cTzVg/

like image 159
sandeep Avatar answered Oct 21 '25 15:10

sandeep


This isn't easy because there is no consistency across browsers in which event is appropriate to do what you need doing. However you say from your tags you're using jQuery, so here's a solution using state tracking to revert depending on your function return:

var currentradio= $("input[name='pick_up_point']:checked")[0];
$("input[name='pick_up_point']").change(function(event) {
    var newradio= $("input[name='pick_up_point']:checked")[0];

    if (newradio===currentradio)
        return;
    if (confirm('Your question here')) {
        currentradio= newradio;
    } else {
        currentradio.checked= true;
    }
});
like image 21
deed02392 Avatar answered Oct 21 '25 15:10

deed02392