I'm trying to learn various aspects of jQuery. I have a button with an id='contact' that I want to change its image on a mouse hover event.
<button id='contact' class='CXIII' onclick="btnContact()">
<img id="logo" src="images/contact_blue.png">
</button>
I am using the following code, but when I hover over the button, I get this error:
Uncaught TypeError: Cannot read property 'replace' of undefined
(anonymous function)
x.each.x.event.special.(anonymous function).handle
x.event.dispatch
x.event.add.y.handle
$("#contact").hover(
function () {$(this).src = this.src.replace(contact_blue.src, contact_green.src);},
function () {$(this).src = this.src.replace(contact_green.src, contact_blue.src);}
);
contact_blue and contact_green are global variables that hold references to images and are instantiated in a preloading function that I have tested and am confident in.
contact_green = new Image();
contact_blue = new Image();
contact_green.src = "http://www.xxxxxx.com/update/images/contact_green.png";
contact_blue.src = "http://www.xxxxxx.com/update/images/contact_blue.png";
What am I doing wrong? Thanks!
p.s. I know that there are other ways to approach this (like CSS), but I am specifically trying to learn jQuery at the moment. Also,
EDIT: I tried both of these, unsuccessfully:
$("#contact").hover(
function () {this.src = this.src.replace(contact_blue.src, contact_green.src);},
function () {this.src = this.src.replace(contact_green.src, contact_blue.src);}
);
$("#contact").hover(
function () {$(this).prop('src') = this.src.replace(contact_blue.src, contact_green.src);},
function () {$(this).prop('src') = this.src.replace(contact_green.src, contact_blue.src);}
);