Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable/disable of textbox on option selected from drop down menu

What I want is that the text box is only accessible if a certain option is picked from the drop down menu and I have an html form as below:

 <tr>
 <td>a.&nbsp;Did any  of your staff participate in training or orientation sessions related to any aspect of social performance management, during the reporting year? </td>
 <td >
 <p>
   <select name="mfi_4_a_i">
     <option>Yes</option>
     <option>No</option>
     <option>No, but planning in future</option>
   </select>
 </p>
 <p>if not,and not planning please explain why not </p>
 <input type="text" name="mfi_4_a_ii" id="sdd" />
 </tr>

Now when a user selects the option No, but planning in future then the text box must be enabled otherwise the textbox must be disabled.

How do I accomplish this?

like image 440
Nyaro Avatar asked Mar 10 '11 10:03

Nyaro


People also ask

How do I activate a TextBox if I select an option in drop down box?

When an item (option) is selected in DropDownList (HTML SELECT), the EnableDisableTextBox JavaScript function is executed. Inside this function, based on whether the Other option (item) is selected, the TextBox will be enabled else disabled. The HTML Markup consists of a DropDownList (HTML SELECT) and a TextBox.

How do I show hidden TextBox when selected from a DropDownList?

The Panel control is hidden by setting Visible property to False. Do you have Passport? When an item (option) is selected in the DropDownList, the selected value is checked. If the selected value is Y (Yes), then the TextBox will be visible else the TextBox will be hidden.

How do I disable a specific item in a dropdown element?

The drop-down is used to create a list of items that need to select an element. We use <select> and <option> elements to create a drop-down list and use disabled attribute in <select> element to disable the drop-down list element.

How do you enable disable a dropdown based on value in another dropdown?

prop("disabled", true); } else $("#ddl2"). prop("disabled", false); }); }); The above code will disable the "ddl2" dropdown on select of a particular value in first dropdown. And will enable it if you select another value.


2 Answers

You should call the javascript function for this.

<select id="mfi_4_a_i" name="mfi_4_a_i" onChange="changetextbox();">
    <option>Yes</option>
    <option>No</option>
    <option>No, but planning in future</option>
</select>

<input type="text" name="mfi_4_a_ii" id="sdd" />

<script type="text/javascript">
function changetextbox()
{
    if (document.getElementById("mfi_4_a_i").value === "noy") {
        document.getElementById("sdd").disable='true';
    } else {
        document.getElementById("sdd").disable='false';
    }
}
</script>
like image 89
Awais Qarni Avatar answered Oct 14 '22 21:10

Awais Qarni


For me the document.getElementById("sdd").disabled='false' did not work so I used

document.getElementById("sdd").disabled='';

if (document.getElementById("mfi_4_a_i").value === "noy") {
    document.getElementById("sdd").disabled='true';
} else {
    document.getElementById("sdd").disabled='';
}
like image 24
Mahesh Avatar answered Oct 14 '22 22:10

Mahesh