Rajiv Rajiv - 2 years ago 113
jQuery Question

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 / dropdown. Below is the scenario:

Radio Button: Daily and Misc
Select: Deity Name

If the user selects Misc then Select (Deity Name) should be disabled and vice versa.

I have already imported the JQuery on my page.


!-- JQuery 1.12.3 JS -->
<script src="../js/jquery-1.12.3.min.js"></script>


<td class="col-md-4"><label class="control-label">Pooja Type</label></td>
<td class="col-md-8" align="center">
<label class='radio-inline'><input type='radio' name='poojaType' value='daily' checked>Daily</label>
<label class='radio-inline'><input type='radio' name='poojaType' value='misc'>Misc</label>
<td class="col-md-4"><label class="control-label" for="deity">Deity Name</label></td>
<td class="col-md-8"><select class="form-control" name="deityName">

Please advice.

Answer Source

Follow the code:

Assume you have two radio buttons as below:

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

and one dropdown as below:

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

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);
    else if ($(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);

I'm sure this will help you. If does then don't forget to mark my answer... ;)


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download