Search code examples
jqueryjquery-attributes

jQuery - update image url only folder


How can I update, Image URL only some part of it...

For Eg: I have 2 folders, if images coming from lenovo folder, I want change that folder to samsung

<img src="images/lenovo/abc.jpg"> to <img src="images/samsung/abc.jpg">


Solution

  • $(function() {
    $("img").each(function() {
    var imgURL = $(this).attr('src');
    if(imgURL.lastIndexOf('/')>6) {
    imgURL = imgURL.replace("images/lenovo","images/samsung");
    }
      });
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
    <img src="images/lenovo/abc.jpg"><img src="images/xyz.jpg">