I'm working on a CMS. When a user wants to replace an existing thumbnail image, they can upload a replacement, and check a checkbox to certify that they are indeed replacing the image. The checkbox seems necessary because otherwise the form submits a blank value (due to the blank default value of the upload field, a security default for all browsers) and wipes out the existing image without a replacement image being specified.
The problem is that I keep forgetting to manually activate the checkbox and the uploaded image does not actually replace the existing one because without the checkbox being checked, the receiving PHP file does not (by design) process the uploaded filename. I suspect that users will have the same problem and I want to keep this from happening by reducing the number of steps required. One solution would be to check the checkbox with jQuery whenever the upload field is clicked. So why doesn't this work:
$('#image_req').click(function () {
if ($('#checkbox_req').attr('checked',true)) {
$('#checkbox_req').removeAttr('checked');
alert('Unchecked');
} else if ($('#checkbox_req').attr('checked',false)) {
$('#checkbox_req').attr('checked','checked');
alert('Checked');
}
});
The HTML in question looks like this:
<input id="image_req" name="local_filename" type="file" />
<input id="checkbox_req" name="replace_image" value="yes" type="checkbox" />