dimanche 5 juillet 2015

jQuery - Forms - Checkbox: checking / unchecking the checkall box

In all honesty, i'm a hobbyist at best when it comes to web development. I've been doing PHP for quite some time but when it comes down to jQuery, I'm an all out newbie! Thanks to the many questions asked on this topic before, I've got a working script.

Problem: Wanting to check all checkboxes when the checkbox w/id=checkall is checked.

Problem 2: Wanting to uncheck the "checkall" box when any other checkbox is unchecked.

Solution:

$(document).ready(function () {
$('#checkall').on('click', function () {
    if ($(this).is(':checked')) {
        $('input[type="checkbox"]').each(function () {
            console.log(this);
            this.checked = true;
        });
    } else {
        $('input[type="checkbox"]').each(function () {
            this.checked = false;
        });
    }
// Uncheck "Checkall" if any other checkbox is unchecked
    if ($('#checkall').is(':checked')) {
        $('input[type="checkbox"]:not("#checkall")').click(function () {
            console.log(this);
            $('#checkall').attr('checked', false);
        });
    }
});

Reason i'm posting: Again - new with jQuery and i'm not sure if what i've written is really and truly "production value" code, but it is operational. It's not a matter of life and death, it's really just a personal project i'm working on that will likely be used by myself and some friends only. I am in the game to learn though and if the code can be improved upon please point me in the right direction to read about it more in depth. I've only been able to find bits and pieces on this topic. Thanks for any help on this!




Aucun commentaire:

Enregistrer un commentaire