changing img src with JQuery but leave pathing info intact?

Posted by Kevin Won on Stack Overflow See other posts from Stack Overflow or by Kevin Won
Published on 2010-03-17T18:32:08Z Indexed on 2010/03/17 18:41 UTC
Read the original article Hit count: 257

Filed under:
|
|

I'm using JQuery to switch out an image src thusly:

$("#myImg").attr("src", "../../new.gif");

notice the relative pathing on the new src. Unfortunately, this isn't portable when I deploy my app. In my MVC app I'm using a ResolveUrl() method that will fix the pathing problem for me so it's portable, but now my JQuery image src swapper doesn't work right since it now switches the correctly resolved path to a broken relative one.

<img id="myImg" src="<%=ResolveUrl("~/Images/transparent.gif")%>" />

What I want is for JQuery to just flip the actual filename and leave the path untouched. My first thought would be to

// pseudocode javascript jquery on my thought on how to approach this prob
var oldFullPath = $('#myImg").GetTheImgSrc;
var newFileNameWithPathIntact = someRegexAddNewFileNameWithOldPath
$("#myImg").attr("src", newFileNameWithPathIntact);

but that seems rather gross and un-JQuery to me. Anyone got a better way?

© Stack Overflow or respective owner

Related posts about mvc

Related posts about jQuery