Yehuda Gutstein
Yehuda Gutstein

Reputation: 381

How to trigger a radio button ONLY by clicking a select option

I am looking to have a radio button that is ONLY triggered by the "onclick" of a select option. I do not want the user to be able to manually change the radio button. Is there a way to make this happen. This is the code I have for the radio button to change (it works), but the user may still change it after the fact:

$("#button1").prop('checked',true);
$("#strategies").mouseup(function() {
    if(strat == $("#strategies").val()) {
        $("#button1").prop('checked',true);
        $("#why").hide();
        $("#whyNo").show();
    }
    else {
        $("#button2").prop('checked',true);
        $("#whyNo").hide();
        $("#why").show();
    }

});

html:

<input type="radio" id="button1" name="switch" value="0">No Switch<br>
<input type="radio" id="button2" name="switch" value="1">Switch<br>

Any suggestions would be greatly appreciated.

Upvotes: 0

Views: 132

Answers (2)

Paul
Paul

Reputation: 3884

In HTML, there's an attribute for input fields called disabled. I'm not certain how to manipulate it in Javascript, however.

<input type="radio" disabled>

or

<input type="radio" disabled="disabled">

Keep in mind, some browsers may render it as "greyed out."

w3schools input tags

w3c recommendations

Upvotes: 3

federicot
federicot

Reputation: 12341

Have you tried this?

$('input[name="switch"]').click(function () {
    return false;
});

Upvotes: 0

Related Questions