Yana Rina
Yana Rina

Reputation: 75

How to enable more than one condition in dropdown list?

I have one dropdown list. When I select "Others", "Other Action textbox" will enable.
When I select "Classroom Training" or "On Job Training(OJT)", dropdown "Proposed Training in Ilsas" and textbox "Proposed Training in Public" will enable.
For the "Others" condition I have made it right.

Then, how can I add another conditions into JavaScript? This is because I have tried to combine the conditions into JavaScript but it's not successful.

<script language="javascript" type="text/javascript"> 
    function otherAction(val) {
        var otherAct = document.getElementById('otherAct');
        if(val=='Others') {
            otherAct.style.display = "block";
        } else {    
            otherAct.style.display = "none";
        }
    }
</script>
<table>
    <tr>
        <td>Action: </td>
        <td>
            <select name="perAction" onchange="otherAction(this.value)"> 
                <option value="0"></option>                    
                <option value="Classroom Training">Classroom Training</option>
                <option value="Coaches and Mentoring by IM">Coaches and Mentoring by IM</option>
                <option value="On Job Training (OJT)">On Job Training (OJT)</option>
                <option value="Others">Others</option>
            </select>
        </td>
        <td>Other Action: </td>
        <td><input name="otherAct" type="text" id="otherAct" /></td>
        <td>Proposed Training in Ilsas: </td>
        <td>
            <select name = "perIlsas">
                <option value="0"></option>                    
                <option value="Project Management">Project Management</option>
                <option value="Contract Management">Contract Management</option>
                <option value="Analytical Skill">Analytical Skill</option>
            </select>
        </td>
        <td>Proposed Training in Public: </td>
        <td><input name="pubTraining" type="text" id="pubTraining" /></td>
    </tr>
</table>

Upvotes: 1

Views: 94

Answers (1)

Abhinav
Abhinav

Reputation: 8168

You just have to use multiple if-else conditions

<script language="javascript" type="text/javascript"> 
    function otherAction(val) {
        var otherAct = document.getElementById('otherAct');
        if(val=='Others') {
            otherAct.style.display = "block";
        } else if(val == 'On Job Training (OJT)'){    
             otherAct.style.display = "none";
             pubTraining.style.display = "block";
            // set display here either `block` or `none` according to your need
        }
        else{
             // set display here either block or none according to your need
        }
    }
</script>

EDIT: First of all give some id attribute to that select box. and then,You can write another else-if condition checking like this

<select id="perIlsas" name = "perIlsas"> //assigning id attribute

else if(val == 'Coaches and Mentoring by IM'){ //another else-if condition    
       $('#perIlsas').prop('disabled', 'disabled'); //this will disable entire selectbox
        }

You can get to know more about prop in the jQuery Docs

Upvotes: 1

Related Questions