简体   繁体   中英

src is undefined variable on image swap

Having problems with a (probably) simple task of passing a variable as a src of an image.

The variables are defined at random from an array, and then if URL criteria is met then that random variable is assigned as the src of the image.

jQuery:

var imgpathnamesfor7offers = ["offers1.jpg", "offers2.jpg", "offers3.jpg", "offers4.jpg"];
var randompathname = imgpathnamesfor7offers[Math.floor(Math.random() * imgpathnamesfor7offers.length)];
var produrl = jQuery(location).attr('href');
var icatRef = produrl.split("/")[4];
if (icatRef == "shark-steam-cleaning") {
    jQuery("#prodpagesliderbar-img").attr("src", jQuery("#prodpagesliderbar-img").attr("src").replace(randompathname));
}

Link: http://jmldirect.uat.venda.com/uk/shark-steam-cleaning/shark-lift-away-steam-pocket-5-in-1/invt/s06lt30100000001

Can anyone see what I'm doing wrong? Console appends 'undefined' to the end of the src.

instead of this:

jQuery("#prodpagesliderbar-img").attr("src").replace(randompathname)

this should be:

jQuery("#prodpagesliderbar-img").attr("src", randompathname)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM