admin管理员组

文章数量:1312741

Is there a way that I can have an image replace a checkbox, and when someone clicks on it, it selects it, and changes the image to another image like a "checked" image?

So essentially:

[heart-icon] Health and Fitness

user clicks on it

[check-icon] Health and Fitness

and now that area is selected

Is there a way that I can have an image replace a checkbox, and when someone clicks on it, it selects it, and changes the image to another image like a "checked" image?

So essentially:

[heart-icon] Health and Fitness

user clicks on it

[check-icon] Health and Fitness

and now that area is selected

Share Improve this question asked Feb 4, 2011 at 2:53 ParkerParker 111 gold badge1 silver badge2 bronze badges 1
  • You might want to check Help with a pure CSS Checkbox Image replacement?. – IsmailS Commented Aug 11, 2011 at 10:46
Add a ment  | 

3 Answers 3

Reset to default 3

There are definitely scripts/plugins available to do this. I've not used any of them so I can't remend one specifically, but here's an example:

http://www.itsalif.info/content/ezmark-jquery-checkbox-radiobutton-plugin

I would try this plugin: SimpleImageCheck http://www.jkdesign/imagecheck/

Code to customize the checkbox looks simple:

$('#checkbox').simpleImageCheck({
  image: 'unchecked.png',
  imageChecked: 'check.png'
  afterCheck: function(isChecked) {
    if (isChecked) {
      // do something
    }
  }
});

No need of plugins nor JQuery:

<span onclick="changeCheck(this)">
<img src="http://www.clker./cliparts/e/q/p/N/s/G/checkbox-unchecked-md.png">
<img src="http://www.clker./cliparts/4/h/F/B/m/4/nxt-checkbox-checked-ok-md.png">
<input type="checkbox" name="chk">
</span>
<script>
function changeCheck(target)
{
    chk = target.lastElementChild;
    chk.checked = !chk.checked;
    target.children[+ chk.checked].style.display = '';
    target.children[1 - (+ chk.checked)].style.display = 'none';
}
function initCheck()
{
    chk = document.getElementsByName('chk')[0];
    chk.style.display = 'none';
    chk.parentNode.children[1 - (+ chk.checked)].style.display = 'none';
}
initCheck();
</script>

本文标签: javascriptReplacing Checkbox with image and update onClick (possibly jQuery)Stack Overflow