Skip to content Skip to sidebar Skip to footer

Enable Disable Select / Dropdown On Radio Selection

I am very new to JQuery, please consider me as a novice. I have a PHP form where in I have a Radio button. Based on the which radio is selected I would like to disable the select /

Solution 1:

Follow the code:

Assume you have two radio buttons as below:

<inputtype='radio' name='radios'id='rdbDaily' value='Daily' />&nbsp;Daily
<inputtype='radio' name='radios'id='rdbMisc' value='Misc' />&nbsp;Misc

and one dropdown as below:

<selectid='selectordropdown'><option>Deity Name</option></select>

you can use below jquery to enable or disable your dropdown by selecting radio buttons:

$('input:radio[name="radios"]').change(function() {
    if ($(this).val()=='Daily') {
        $('#selectordropdown').attr('disabled', false);
    } 
    elseif ($(this).val()=='Misc') {
        $('#selectordropdown').attr('disabled', true);
    }
});

or you can also use below jquery to enable / disbale your dropdown:

$("#rdbDaily").click(function() {
    $("#selectordropdown").attr("disabled", false);
});

$("#rdbMisc").click(function() {
    $("#selectordropdown").attr("disabled", true);
});

Solution 2:

You can just get the checked value on change event of radio button and set it against the attribute disabled value as below

$('input[name="poojaType"]').on('change', function() {
  $('select[name="deityName"]').attr('disabled', this.value != "daily")
});
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><tr><tdclass="col-md-4"><labelclass="control-label">Pooja Type</label></td><tdclass="col-md-8"align="center"><labelclass='radio-inline'><inputtype='radio'name='poojaType'value='daily'checked/>Daily</label><labelclass='radio-inline'><inputtype='radio'name='poojaType'value='misc' />Misc</label><selectname="deityName"><option>Value 1</option><option>Value 2</option><option>Value 3</option><option>Value 4</option></select></td></tr>

Solution 3:

Below is the code for JQuery:

<script>
$(':radio[name=poojaType]').change(function () {
var prop = this.value == 'misc';
$('select[name=deityName]').prop({ disabled: prop, required: !prop });
});
</script>

Post a Comment for "Enable Disable Select / Dropdown On Radio Selection"