I'm looking for help with a script that will hide my checkboxes and replace them with styled checkbox images. The script I have hides the checkboxes and displays the default image, but it doesn't update the checkbox state or image when clicked. I think I may be missing something simple, as I am still learning jQuery.
Here's the script I'm using:
$(".check").each(function() {
$(this).hide();
var $image = $("<img src='assets/images/layout/checkbox/unchecked.png' />").insertAfter(this);
$image.click(function() {
var $checkbox = $(this).prev(".check");
$checkbox.prop('checked', !$checkbox.prop('checked'));
if($checkbox.prop("checked")) {
$image.attr("src", "assets/images/layout/checkbox/checked.png");
} else {
$image.attr("src", "assets/images/layout/checkbox/unchecked.png");
}
})
});
Here is the HTML code:
<input type="checkbox" class="check" />
Edit: Is this method of styling checkboxes the best approach? I haven't found anything easier, so I would appreciate any suggestions.