Replace img src with jQuery

Simple we use to show a image by writing the following code:

<img src ="thelocationofyourimage.jpg" >

Now if sometime you need to change this src attribute value by jQuery. Then you need to put down the following code.

$('img[src^="thelocationofyourimage.jpg"]').each(function() {
   $(this).attr('src', $(this).attr('src').replace("thelocationofyourimage.jpg", "UpdatedLocationOfTheImage.jpg"));

Try it by yourself. Thanks in advance.

Shaharia is a professional software engineer with more than 10 years of experience in the relevant fields. Digital ad certified, cloud platform architect, Big data enthusiasts, tech early adopters.

Site Footer