Nikola Nikola - 4 months ago 8
jQuery Question

Is there an event just before the radio button is about the be checked

This is the part of the code for one of the radio buttons:

$("#new").click(function(){
if( $('#new').is(':checked') )
$.jGrowl("You already have this option selected!", life: 1500});
else
changeOption(1);
});


Now, I understand that the
else
part will never run, because the radio button will be already checked on click event. What I'm wondering is, is there an event which would let me capture the state of the radio button (which is about to be clicked) and therefor determine if he is not yet clicked, and if so change the option to this newly selected one.

Answer

Use the mouseup event instead.

$("#new").mouseup(function(){
    if( $('#new').is(':checked') )
        $.jGrowl("You already have this option selected!", life: 1500});
    else
        changeOption(1);
});

The fiddle.

Edit
Even though the mouseup event works, it seems more logical to use the mousedown event. Here's that fiddle.