How to add a onclick attribute to a list of checkboxes?

Store the ID in a variable, and access that variable using JavaScript:

<script>
document.getElementById("<?php echo $checkbox_id; ?>").onclick = function() {
    chk(electronics, this.checked);
}
</script>

You can also collect multiple IDs in an array and run through that array later in your script. But avoid the onclick attribute in markup, this is bloat and bad code style. 🙂