I am trying to replace an img path in jquery (injecting into a remote page)
replace example.com/thumbs
with example.com/images
I have tried this but it doesn't seem to work.
$("img").attr("src").replace("thumbs", "images");
I am trying to replace an img path in jquery (injecting into a remote page)
replace example.com/thumbs
with example.com/images
I have tried this but it doesn't seem to work.
$("img").attr("src").replace("thumbs", "images");
Step 1: script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js">
Step 2:
This is what you wanna do:
You need to update the attribute with the returned value for that you can use a callback function as the second argument in
attr()
method where the second argument holds the current attribute value.The above method will iterate over the
img
tags if there are multipleimg
elements so you can avoid usingeach()
method for iterating.This gets the value, but doesn't set it back to the attribute:
That requires another step, something like:
Or, if you want a single line:
Look at this one, you wasn't setting src for an image.