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.