mardi 7 juillet 2015

Generalize a specialized JQuery function

This is a question about Multiple Checkbox Select/Deselect Using JQuery: http://ift.tt/TPTNzT

which has the functionality of selecting multiple items from a list to process them. The Online Demo is at: http://ift.tt/1G5KSlS

I've extended the demo to include several groups, and have made the sample JQuery JavaScript function to work for one case, the BlackBerry one. The working code is at http://ift.tt/1gmoxvR.

In it, this is the specialized function that I want to generalize:

$(function () {
    $("#BlackBerry").click(function () {
        $('.case_blackberry').attr('checked', this.checked);
    });
    $(".case_blackberry").click(function () {
        if ($(".case_blackberry").length == $(".case_blackberry:checked").length) {
            $("#BlackBerry").attr("checked", "checked");
        } else {
            $("#BlackBerry").removeAttr("checked");
        }
    });
});

I want to generalize it so that the "#BlackBerry" and ".case_blackberry" are two parameters to the function, so that I can call it several times to cover other groups as well.

I'm not a JavaScript developer. Please help.

Also, did I adapted correctly? For this line:

<input type="checkbox" class="case_blackberry" name="case" value="BlackBerry Bold 9650">

Should I have changed class="case_blackberry" or name="case" instead?

Thanks




Aucun commentaire:

Enregistrer un commentaire