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?
Hint here: question tags ;)
ohnoes
2009-01-13 11:35:00
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
2009-01-13 10:44:34
+1
A:
Try this:
$("img").attr("src", function (val) {
return "images/" + val;
})
ohnoes
2009-01-13 10:48:46
yeah that'd be it :) one of these days I'll have to start caring about jquery
annakata
2009-01-13 10:51:54
+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
2009-01-13 10:59:38