perkes456
perkes456

Reputation: 1181

On checkbox click storing value into an array

I have an checkbox list like following:

$('.check-user').click(function() {
    var $this = $(this);
    var user_id = $this.attr('value');
    var brandContainer = $this.closest('.pop-state').siblings('.pop-brands');
    brandContainer.html('<p style="margin: 10px !important;">Processing... </p>');
    $.post('/user/usersBrands', { userId: user_id } , function(data) {

        var brands = JSON.parse(data);
        var container = $('<div class="pop-mission-co" />');

        brands.forEach(function (brand, index) {
            var isRegisteredToBrand = null;
            if(brand.userId==null)
            {
                isRegisteredToBrand = false;
             }
            else{
                isRegisteredToBrand = true;
            }
            container.append(buildBrand(brand.id, brand.name, isRegisteredToBrand, index));
        });
        function buildBrand(id, name, isActive, index) {
            var isChecked = isActive ? 'checked="checked"' : ' ';
            return $(
                '<div class="pop-mission-to">' +
                    '<label>' +
                        '<input class="check-brand"' +
                            isChecked +
                            'type="checkbox"' +
                            'name=""' +
                            'value="' + id + '"' +
                            'data-id="' + index + '">'
                                + name +
                    '</label>' +
                '</div>'
            );
        }
        brandContainer
            .children('p').remove();
        brandContainer
            .html(container)
            .find('.pop-mission-co').show();
    });
    brandContainer.on('change', '.check-brand', function() {

        console.log($(this).attr("checked"));

    });
});

The checkbox list is created after a $.post in jQuery and now I need to handle the event when each of these checkboxes is checked like following:

 brandContainer.on('change', '.check-brand', function() {

            console.clear();
    var checked = "";
    if($(this).attr("checked"))
    {
        checked = "checked";
    }
    else
    {
        checked = "unchecked";
    }
    console.log(checked);
    console.log($(this).val());

        });

As you can see I'm using a delegated event handler to check whether the checkbox has been checked or not... When a user clicks on 1 checkbox I should put it in the array like following:

Array { "checked", "1" }

When user clicks on another checkbox the array should be updated like this:

Array {  "checked", "1", "checked", "2" }

If the user unchecks the checkbox that he already checked, lets say second one with value 2, the array should look like this:

Array { "checked", "1", "unchecked", "2" }

The process goes on... Can someone help me out with this ? Thanks a lot ! :)

Upvotes: 0

Views: 481

Answers (1)

phreakv6
phreakv6

Reputation: 2165

I think this might work.

var checkbox_events = [];
brandContainer.on('change', '.check-brand', function() {
    if($(this).is(':checked') {
        checkbox_events[$(this).val()] = 'checked';
    } else {
        checkbox_events[$(this).val()] = 'unchecked';
    }
});

Upvotes: 1

Related Questions