tags:

views:

83

answers:

5

I would like to add a path "images/" to all img tags in my html file. Is that possible and if how can I achive that?

+1  A: 

From what? Is search/replace in notepad good enough?

Neil Coffey
Hint here: question tags ;)
ohnoes
A: 

I'm sure there's a jQuery specific way to do this, but this basic JS loop will do it. Kind of a strange thing to do after loading all the images from a different URL already though.

var a = document.getElementsByTagName('img');

for(var i=0, n=a.length; i<n; i++)
{
  a[i].src = 'images/'+a[i].src; //see note
}

note: in practice I think you'll want a regex replace to insert the 'images/' string here

annakata
+1  A: 

Try this:

$("img").attr("src", function (val) {
          return "images/" + val;
        })
ohnoes
yeah that'd be it :) one of these days I'll have to start caring about jquery
annakata
+1  A: 

I think this will work well:

$("img").each( function() {
    $(this).attr("src", "images/" + $(this).attr("src"));
});

note that it has sense only if you have already relative path to your images.

tanathos
A: 

Perfect, thanks a lot :-)