Replacing Checkboxes and Radio Button’s Programmically with Javascript
There is a great article and javascript on CodeProject.com. The code was originally written by BrainError.
I had to add support for labels, however. If you had a label like this:
<label for="CheckBoxID"></label>
it wouldn’t update the image if you clicked it. I added the following to the js file:
In function replaceChecks, I added one line: inputs[i].onclick = new Function(‘imgChange(‘+i+’)');
Here’s the relevant section of code:
//set image
if(!inputs[i].disabled) {
img.onclick = new Function('checkChange('+i+')');
inputs[i].onclick = new Function('imgChange('+i+')');
}
I also had to add a separate function here:
//change the image if the checkbox is changed.
function imgChange(i) {
if(inputs[i].checked) {
document.getElementById('checkImage'+i).src=imgCheckboxTrue;
} else {
document.getElementById('checkImage'+i).src=imgCheckboxFalse;
}
}