I'm trying to make a function that I can use globally to create rollover states for images. Basically, I want to be able to add a class to an image, and then when you rollover it, jquery will use the same image name, and same extension, but add "-over" to the file name. (I'll have an image with the rollover state named the same as the non rolled over state except with the -over on it. I came up with this, but it's not working. Am I doing something wrong or does anyone know of a better way to do it?
$('.btn').hover(function(){
$(this).attr("src").split(".jpg").join("Over.jpg"));
});
image:
<img src="/static/images/overlay-close-button.jpg" alt="Close" title="Close" id="our-staff-overlay-close" class="btn"/>
Thanks!
EDIT: Is there any way to make it non specific to the file time, where it can figure out any file type rather than just jpgs?
I'm using:
$('.btn').hover(function(){
this.src = this.src.split(".jpg").join("Over.jpg");
}, function() {
this.src = this.src.split("Over.jpg").join(".jpg");
});
and it's working great
EDIT 2: Can I also add an active state (when the button is being clicked)?