Thursday, 22 June 2017

jQuery make checkbox checked on select's selected option



HTML

<div>
    <select name="id_dystrybutor_glowny" id="id_dystrybutor_glowny" value="">
        <option value="">select one</option>
        <option value="1">Dyst1</option>
        <option value="2">Dyst2</option>
        <option value="3">Dyst3</option>
    </select>
</div>
<input type="checkbox" name="id_dystrybutor[]" class="dystrybutor" value="1"> Dyst1
<br/>
<input class="dystrybutor" type="checkbox" name="id_dystrybutor[]" value="2"> Dyst2
<br/>
<input class="dystrybutor" type="checkbox" name="id_dystrybutor[]" value="3"> Dyst3
<br/>


JS

$(document).ready(function() {
    $('#id_dystrybutor_glowny').change(function() {
        var dID = $(this).find(":selected").val();
        $('.dystrybutor[value="' + dID + '"]').prop('checked', true)
            .siblings().prop('checked', false);
    });
});

1 comment:


  1. Lovely blog with much more interesting article, I will keep on reading your update. Thanks for the share Ear Plugs for Swimming Ear plugs for Sleeping Custom Ear Plugs

    ReplyDelete