lundi 9 avril 2018

Mapping groups of Checkbox values to input fields using jQuery

I have multiple groups of checkboxes which I wish to copy the values into a text field for each respective group of checkboxes.

I had found a good answer previously (below), however this only seems to work for ALL checkboxes in the form, when I want to get the values in groups. I have tried applying this method by also assigning custom classes to the groups of checkboxes so I can capture them individually, but have not had any success.

WORKING CODE

$checks = $(":checkbox");
    $checks.on('change', function () {
        var string = $checks.filter(":checked").map(function(i,v){
            return this.value;
        }).get().join(", ");
        $('#testField').val(string);
    });

HAVE TRIED w/ no luck

$checks = $(".customClass");
    $checks.on('change', function () {
        var string = $checks.filter(":checked").map(function(i,v){
            return this.value;
        }).get().join(", ");
        $('#testField').val(string);
    });




Aucun commentaire:

Enregistrer un commentaire