I have two versions of an image - a greyed out image and a color version of the same image. In the normal state, the image is greyed out. When hovered or clicked on, the image becomes the colored version.
I know this can be done with either toggleClass
/addClass
/etc or changing the attr(src)
. I've managed to have this working with hover, but I cannot get the click element to work.
These are the images and the page I am trying to work with - http://www.expatlifeinsurance.com/contact-health-insurance/
This is the code I have right now:
<div id="smokers">
<img id="nosmoke" src="http://www.expatlifeinsurance.com/images/smoke_no_grey.png">
<img id="smoke" src="http://www.expatlifeinsurance.com/images/smoke_grey.png">
</div>
$("#nosmoke")
.mouseover(function() {
this.src = this.src.replace('http://www.expatlifeinsurance.com/images/smoke_no_grey.png', 'http://www.expatlifeinsurance.com/images/smoke_no_green.png');
})
.mouseout(function() {
this.src = this.src.replace('http://www.expatlifeinsurance.com/images/smoke_no_green.png', 'http://www.expatlifeinsurance.com/images/smoke_no_grey.png');
})
.click(function() {
$(this).toggleClass("off");
if ($(this).hasClass("off")) this.src = this.src.replace(/(.*)-on(.*)/, "$1-off$2");
else this.src = this.src.replace(/(.*)-off(.*)/, "$1-on$2");
}
);