Skip to content Skip to sidebar Skip to footer

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: <

Solution 1:

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

Solution 2:

Try this:

DEMO

JS

functionsetChk(value){
    var chk = document.getElementById('check1');
    chk.checked = (value != 'null');
}

HTML

<table><tr><td><inputtype="checkbox"name="check1"id="check1"/>Process 1:</td><td><selectid="process1"name="process1"onchange="setChk(this.value);"><optionvalue="null">--Select Process--</option><optionvalue="NameA">NameA</option><optionvalue="NameB">NameB</option><optionvalue="NameC">NameC</option></select></td></tr></table>

Solution 3:

jQuery would be easier, but in Javascript, try adding an onchange attribute to the select:

onchange="checkValue()"

Then in javascript:

functioncheckValue() {
   var e = document.getElementById("process1");
   var value = e.options[e.selectedIndex].value;
   if (value == "null") {
      document.getElementById("check1").checked=false;
   } else {
      document.getElementById("check1").checked=true;
   }
}

Just add an id to the checkbox id="check1"

Solution 4:

This answer is really cool!

But if you open that would use jQuery then the code would look like this:

jQuery(document).ready( function() {
    jQuery('#process1').on('change', function() {
        var selectedVal = jQuery(this).find('option:selected').val(),
            checkbox = jQuery('input[name="check1"]');
        if (selectedVal !== 'null') {
            checkbox.attr('checked', 'checked');
        } else {
            checkbox.removeAttr('checked');
        }
    });
});

Demo

Post a Comment for "Check A Checkbox On A Dropdown Selection"