1

I have a list of data that I want to put in a checkboxList.

$data = [
    ['id' => '1', 'name' => 'emel'],
    ['id' => '2', 'name' => 'makluman'],
    ['id' => '3', 'name' => 'memo'],
];

$list = ArrayHelper::map($data, 'id', 'name');

return Html::checkboxList(
    'roles',
    [],
    $list,
    [
        'itemOptions' => [
            'onclick' => 'myFunction()',
            'id' => 'check',
        ],
    ]
);

I can return the result, the problem is I can't get the data that I checked.

Below is my script code:

<script>
function myFunction() {
    result = $('input[type="checkbox"]:checked').val();
    console.log(result);
}
</script>

When I check the first box it will get the value 1, but when I check the second box it will return 1 when it's supposed to return the value 2. When I uncheck the first box then it returns 2.

I want it to save in one var. For example:

I check first box,

a = ["1"]

then I check the second box

a = ["1","2"]

and so on.

Please help me.

0

1 Answer 1

1

Few things you should look at

  1. There are more than 1 elements being selected when you check the second checkbox, you need to iterate all the checked boxes and push into an array by using jquery's $.each() otherwise it will just return the first element value.

  2. You should use event delegation for binding click see Why.

  3. You are providing an id via itemOptions to the checkboxList() which is technically wrong as it would assign that same id to all the checkboxes and you will be experiencing strange behaviors working with them in javascript or html, you should use the item option which takes a callback function ($index, $label, $name, $checked, $value) and provide unique ids to every input.

See the below code how to use all of the above

  • On top of your view add the following

    $js = <<<JS
        function myFunction() {
            var result=[];
    
            $('input[type="checkbox"]:checked').each(function(index,checkbox){
                result.push($(checkbox).val());
            });
    
            console.log(result);
        }
    
        $(document).on("click",'input[type="checkbox"]',myFunction);
    
    JS;
    $this->registerJs($js, \yii\web\View::POS_READY);
    
  • Then update the code for your checkbox to below

    $data = [
        ['id' => '1', 'name' => 'emel'],
        ['id' => '2', 'name' => 'makluman'],
        ['id' => '3', 'name' => 'memo'],
    ];
    
    $list = ArrayHelper::map($data, 'id', 'name');
    
    echo Html::checkboxList('roles', [], $list, [
        'item' => function ($index, $label, $name, $checked, $value) {
            $return = '<label>';
            $return .= '<input type="checkbox" name="' . $name . '" value="' . $value . '" ' . $checked . ' id="check-' . $index . '" />';
            $return .= '<span>' . ucwords($label) . '</span>';
            $return .= '</label>';
            return $return;
        },
    ]);
    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.