Reputation: 73
I am trying to change the string within an attr('src')
in just the beginning of the address, and after the replacement, it keeps the latter information.
Here's the html:
<img src="../folder1/folder2/folder3/picture.png" />
<img src="../folder1/folder2/folder3/picture2.png" />
It would need to perform this after pageload, here's my existing script/scripts:
$(document).ready(function(e) {
// this is one attempt
$('img').each(function() {
var src = $(this).attr('src');
$(this).ready(function() {
$(this).attr('src', src.replace('../', '/'));
}, function() {
$(this).attr('src', src);
});
});
// this is another attempt, not both used at the same time
$('img').attr('src').replace('../', '/');
// I also tried this
$('img').each(function() {
if($(this).attr('src') == '../') {
$(this).attr('src', '/');
};
});
});
The overall goal is to produce this result:
<img src="/folder1/folder2/folder3/picture.png" />
<img src="/folder1/folder2/folder3/picture2.png" />
Upvotes: 1
Views: 3114
Reputation: 104775
You have to get the current src
- update it, then re-assign it:
$('img').attr('src', function(index, src) {
return src.replace('../', '/')
})
Upvotes: 4