Ahamed Zulfan
Ahamed Zulfan

Reputation: 135

Disable Dropdown Box using Javascript on condition

I have a form in which there are a few radio buttons and a dropdown box.

What I want?

  1. When I select the radio button 'Receipt' or 'Payment' the dropdown box must be active.
  2. When I select the radio button 'Other Income' or 'Other Expense', the dropdown box must be disabled.

The HTML and JS I've tried is mentioned below. Any help would be appreciated.

HTML

<label class="radio-inline"><input type="radio" name="type" id="type" value="receipt" onClick="party_check()" />Receipt</label>

<label class="radio-inline"><input type="radio" name="type" id="type" value="payment" onClick="party_check()" />Payment</label>

<label class="radio-inline"><input type="radio" name="type" id="type" value="other income" onClick="party_check()" />Other Income</label>

<label class="radio-inline"><input type="radio" name="type" id="type" value="other expense" onClick="party_check()" />Other Expense</label>

<select name="party" id="party" class="form-control">
<option value="">Please select an option</option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
</select>

JS

function party_check(){
    if((document.type[0].checked) || (document.type[1].checked)){
        document.getElementById("party").disabled=false;
    }
    else
    {
        document.getElementById("party").disabled=true;
    }
}

Upvotes: 3

Views: 14665

Answers (2)

liborza
liborza

Reputation: 1009

U can use something like this :

-Set a ID to id**Yes** and **No**and do that in JS with click function

$("#Yes").click(function() {
  $("#party").attr("disabled", false);
  //$("#discountselection").show(); //To Show the dropdown
});
$("#No").click(function() {
  $("#party").attr("disabled", true);
  //$("#discountselection").hide();//To hide the dropdown
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<label class="radio-inline"><input type="radio" name="type" id="Yes" value="receipt" onClick="party_check()" />Receipt</label>

<label class="radio-inline"><input type="radio" name="type" id="Yes" value="payment" />Payment</label>

<label class="radio-inline"><input type="radio" name="type" id="No" value="other income"  />Other Income</label>

<label class="radio-inline"><input type="radio" name="type" id="No" value="other expense"  />Other Expense</label>

<select name="party" id="party" class="form-control">
<option value="">Please select an option</option>
<option value="1">Option 1</option>
<option value="2">Option 2</option>
</select>

Upvotes: 0

J28
J28

Reputation: 1110

Check this

Html

<label class="radio-inline"><input type="radio" name="type"  value="receipt" onclick="party_check(this)" />Receipt</label>

<label class="radio-inline"><input type="radio" name="type"  value="payment" onclick="party_check(this)" />Payment</label>

<label class="radio-inline"><input type="radio" name="type" value="other income" onclick="party_check(this)" />Other Income</label>
<label class="radio-inline"><input type="radio" name="type"  value="other expense" onclick="party_check(this)" />Other Expense
</label>

<select name="party" id="party" class="form-control">
    <option value="">Please select an option</option>
    <option value="1">Option 1</option>
    <option value="2">Option 2</option>
</select>

Javascript

function party_check(rad){
    if(rad.value == "receipt" || rad.value == "payment"){
        document.getElementById("party").disabled=false;
    }else{

        document.getElementById("party").disabled=true;
    }
}

"id" attribute's value must be unique across the tags in the html page

Upvotes: 4

Related Questions