mardi 26 avril 2016

How to get checked checkbox inside in div using jquery?

I have list of checkbox inside a div few are checked. i want to change background color green of div when if checkbox is checked.

And On non checked checkbox background color should be gray.

HTML Code :

<div class="row">
    <label>
        <div class="col s3" style="padding:10px" id="div39">
            <div>
                <input type="checkbox" name="chk39" value="39" checked="">
                <span>Featured Project</span>
            </div>
        </div>
    </label>
    <label>
        <div class="col s3" style="padding:10px" id="div40">
            <div>
                <input type="checkbox" name="chk40" value="40">
                <span>Specials/Discounts</span>
            </div>
        </div>
    </label>
</div>

Jquery Code:

var check = 1;
    $('div').find('input[type=checkbox]').click(function(event) {
        var val = $(this).val();
        if(check==1)
        {
            $('#div'+val).css({'background-color': 'lightgreen'});
            check=0;
        }else{
            $('#div'+val).css({'background-color': 'lightgray'});
            check=1;
        }
    });

$(document).ready(function($) {
        var selected = [];
        $('input[type=checkbox] :checked').each(function() {
            alert('asd');
            selected.push($(this).attr('value'));
        });
});




Aucun commentaire:

Enregistrer un commentaire