user906153 user906153 - 6 months ago 9
HTML Question

Check a checkbox on a dropdown selection

I'm looking for a way to automatically check a checkbox when a selection is made from a dropdown. The HTML code looks something like this:

<table>
<tr>
<td>
<input type="checkbox" name="check1" />Process 1:</td>
<td>
<select id="process1" name="process1">
<option value="null">--Select Process--</option>
<option value="NameA">NameA</option>
<option value="NameB">NameB</option>
<option value="NameC">NameC</option>
</select>
</td>
</tr>
</table>


If possible, if the user goes back and selects
--Select Process--
, the checkbox would uncheck

What would the Javascript code look like for this?

Answer
$(function(){
    $('#process1').change(function() {
        var selected = $('#process1 option:selected');
        if (selected.val() == "null") {
            $('input[name="check1"]').prop('checked', false);
        }
        else {
            $('input[name="check1"]').prop('checked', true);
        }
    });
});