I am making a checklist. I am adding the checkboxes dynamically via javascript and adding labels for them too.
Now I want when I check a checkbox the background color of the label to change indicating the item has been selected but onClick attribute is not allowed in "label" tag.
So I added the text as a child of checkbox but it is not visible but the onclick event is working on checkbox.
var chked = document.createElement("input");
chked.type = 'checkbox';
chked.value = data[friendIndex].id;
chked.id = friendIndex;
chked.width = '300';
chked.onclick = function (){document.getElementById(this.id).style.backgroundColor='#4285f4';};
var label = document.createElement('label');
label.htmlFor = friendIndex;
label.id = friendIndex;
label.setAttribute('onclick' ,function (){document.getElementById(this.id).style.backgroundColor='#4285f4';});
label.appendChild(document.createTextNode(data[friendIndex].name));
chked.appendChild(document.createTextNode(data[friendIndex].name));
var mybr=document.createElement('br');
chklstTarget.appendChild(chked);
chklstTarget.appendChild(label);
chklstTarget.appendChild(mybr);