views:

355

answers:

3

I am trying to replace an image when a link in another part of the page is rolled over. I have a hard coded version working, but I would like to get this so that when I upload to a server I do not need to go in and change the path part.

$(document).ready(function() {
$('.laundryLinks li a').hover(function() {
 $('.homeLaundryPict').attr('src', 'http://192.168.1.2:8888/karenMaezenMiller/wp-content/themes/karenMillerTheme/i/laundryHome_quote.jpg'); },
 function() {
 $('.homeLaundryPict').attr('src', 'http://192.168.1.2:8888/karenMaezenMiller/wp-content/themes/karenMillerTheme/i/laundryHome_noQuote.jpg'); });
});

PS. I also played with trying to just replace a bit of source but had no luck

$(this).src.replace("_quote","_noQuote");
A: 

You need to do the following to get this to work:

$(this)[0].src = $(this)[0].src.replace(/_quote/, '_noQuote');

Hope it helps!

EDIT

$(function() {
    $('.laundryLinks li a').hover(function() {
        with ( $('.homeLaundryPict')[0] )
            src = src.replace(/_quote/, '_noQuote');
    }, function() {
        with ( $('.homeLaundryPict')[0] )
            src = src.replace(/_noQuote/, '_quote');
    });
});

Here you go ...

aefxx
Thank you aefxx, how do I intergrate .homeLaundryPict into this though? The following did not work. Cheers.$(document).ready(function() {$('.laundryLinks li a').hover(function() { $('.homeLaundryPict').src = $(this)[0].src.replace(/_noQuote/, '_quote'));; }, function() { $('.homeLaundryPict').src = $(this)[0].src.replace(/_quote/, '_noQuote');); });});
Eric C
sorry for the mess of code, it does not see, to let me put it in a code tag
Eric C
See my edit, Eric. Have a nice day.
aefxx
THANK YOU! that solved it.
Eric C
A: 
Gaby
I tried this with no luck, syntax errors?$(document).ready(function() {$('.laundryLinks li a').hover(function() { $img = $('.homeLaundryPict'); $img.attr('src', $img.attr('src').replace("_noQuote","_quote") );, function() { $img = $('.homeLaundryPict'); $img.attr('src', $img.attr('src').replace("_quote","_noQuote") ););});
Eric C
replace(regexp, String) is the correct signature not replace(String, String). See my answer, please.
aefxx
@aefxx, according to http://www.w3schools.com/jsref/jsref_replace.asp it can be either a regex or a string..
Gaby
@Eric C, you close the inner functions with ) instead of } have a look at your code.. I will add it in my answer as update..
Gaby
thank you very much, this is the result of a designer trying to code! Alas something still seems to be missing...
Eric C
the images (img tags) have the `homeLaundryPict` class right ? (you have a live url where we can see it ?)
Gaby
yes here is a live urlhttp://www.karenmaezenmiller.com
Eric C
this shows a maintenance mode page... i meant if you have some url where we can see the page with the problem..
Gaby
that is odd, I turned it off, should be visible now. Cheers.
Eric C
maintenance mode is back on since aefxx provided a great solution below. The site will launch later this week. Thanks all.
Eric C
A: 

Try this (assuming only the first element will swap):

var pic = $('.homeLaundryPict')[0];
var orig = pic.src;
$('.laundryLinks li a').hover(function() {
    pic.src = pic.src.replace(/_quote/,"_noQuote");
}, function() {
    pic.src = orig;
});
David
This looks like it should work, however it does not. Thanks though!
Eric C