'clicked checkbox is not checked but wrong one is getting checked automatically

I have a table with many rows, where the first column is a checkbox. and HTML structure is given below. Each check box has the same classes and the only difference is the value and the last part of the id as given below.

<tr class="even">
  <td class=" col-select col-massaction col-in_category " >
    <label class="data-grid-checkbox-cell-inner" for="id_2001">
       <input type="checkbox" value="2001" id="id_2001" class="checkbox admin__control-checkbox">
       <label for="id_2001"></label>
    </label>
  </td>
</tr>
    

When I click on the last checkbox, one random checkbox in the upper portion gets a tick mark, and it returns its value, Not the one I checked.

It is showing this behavior randomly. Sometimes the last one works fine, but some other shows are wrong.

$(document).on("click", ".checkbox", function () {
    var id = $(this).val();
    console.log(id);
});

What is wrong with this??



Solution 1:[1]

You can use Array.map. Here it is.

let array=['a','b','c']
const res = array.map(item => ({name: item}))
console.log(res)

Solution 2:[2]

Why haven't you tried a simple for loop yet:

let array=['a','b','c']
let ans = []; //initialize empty array
for(let i = 0; i < array.length; i++){ //iterate over initial array
ans.push({name : array[i]}); //push in desired format 
}
console.log(ans);

Solution 3:[3]

For example. With map() or loops like for... or forEach you can do it.

array=['a','b','c']

let res1 = array.map(a => ({name: a}))
console.log('map()', res1)

// or forEach

let res2 = [];
array.forEach(a => {
  res2.push( {name: a})
})

console.log('forEach()', res2)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 baymax
Solution 2 Tushar Shahi
Solution 3 Maik Lowrey