0

While clicking a button/trigger I need to change the folder path name from img/image.jpg to imgdark/image.jpg for all the instances of the specific class ".imageShift". What ever image name that is inserted in the image src should remain the same through out all the occurrences. Just the "img" folder path has to be replaced with "imgdark". I tried a code given below.

$('.themeColor').click(function () {
    var getId = $(this).attr('id');
    var imagePath = $('.imagePath');
    if (getId === 'darkTheme') {

        imagePath.attr('src', function (index, attr) {
            return attr.replace('img', 'imgdark');
        });
    } else if (getId === 'lightTheme') {
        imagePath.attr('src', function (index, attr) {
            return attr.replace('imgdark', 'img');
        });
    }
});

The above code is working perfectly but on each click I am getting a console error as displayed below. I need to get rid of that console error and also make this work as it does already. Please help with this if anyone have done this already.

Uncaught TypeError: Cannot read property 'replace' of undefined
at HTMLLIElement.<anonymous> (homepage.js:1619)
at z (jquery-3.3.1.min.js:2)
at w.fn.init.attr (jquery-3.3.1.min.js:2)
at HTMLSpanElement.<anonymous> (homepage.js:1617)
at HTMLSpanElement.dispatch (jquery-3.3.1.min.js:2)
at HTMLSpanElement.y.handle (jquery-3.3.1.min.js:2)
17
  • 2
    Do you have a working jsFiddle example somewhere where you can reproduce the error in the console? Commented Dec 3, 2018 at 9:36
  • if (getId === 'darkTheme') is there are multiple instances with same id(darkTheme or lightTheme )? Commented Dec 3, 2018 at 9:40
  • 3
    Just check if the attr exists before trying to replace it. There seems to be a case where either the attribute or the imagePath class do not exists, so you cannot apply replace on it. I cannot really help you with a solution as i cant see the whole picture, but try using the debugger and step through your function and see why there are cases where the attribute doesn't exists. Commented Dec 3, 2018 at 9:41
  • you have to check whether the imagePath having values @Constantin Chirila noted this issues. you need to check whether the imagePath have values otherwise you dont allow to replace the values Commented Dec 3, 2018 at 9:49
  • 1
    You have a bug. When you change imgdark to img its actualy changing to imgdarkdark. Because replase changing not whole string but part of it imgdarkdark. You nedd to use regex in your replace Commented Dec 3, 2018 at 9:52

3 Answers 3

1

Use this:

$('.themeColor').click(function () {
var getId = $(this).attr('id');
var imagePath = $('.imagePath');
if (getId === 'darkTheme') {

    imagePath.attr('src', 'imgdark/image.jpg');
} else if (getId === 'lightTheme') {
    imagePath.attr('src', 'img/image.jpg');
}

});

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the reply. Actually I have multiple instances of the class .imagePath in multiple images. So this code should work for many images with class .imagePath and not only for a single image image.jpg. This code works only for image.jpg. I actually need to change the img/ alone to imgdark/ in all the images which has class .imagePath.
what if name of the image is changing? Or there are multiple instances?
OK! test this solution: $('.themeColor').click(function () { var getId = $(this).attr('id'); var imagePath = $('.imagePath'); if (getId === 'darkTheme') { imagePath.attr('src', function (index, attr) { if attr {return attr.replace('img/', 'imgdark/');} }); } else if (getId === 'lightTheme') { imagePath.attr('src', function (index, attr) { if attr { return attr.replace('imgdark/', 'img/');} }); } });
@mdev Thank you so much. This works. I am not even an intermediate and still I cant figure out how that worked. But thanks for your help. It worked with this if block included inside the function. The console error disappeared. Thanks again
1

You could do something like this:

$('.themeColor').click(function () {
  $('.themeColor').removeClass('themeActive');
     var getId = $(this).attr('id');
     var imagePath = $('.imagePath');
     if (getId === 'darkTheme') {
      imagePath.attr('src', imagePath.attr('src').replace('img/', 'imgdark/'));
     } else if (getId === 'lightTheme') {
       imagePath.attr('src', imagePath.attr('src').replace('imgdark/', 'img/'));
     }

    $('.imagePath').each(function(index, val) {
     console.log('You changed the image('+(index+1)+') path to:'+$(this).attr('src'))
    });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button class="themeColor" id="darkTheme">Dark</button>
<button class="themeColor" id="lightTheme">Light</button>


<img class="imagePath" src="imgdark/sampleimg.jpg" alt="">
<img class="imagePath" src="imgdark/sampleimg.jpg" alt="">

1 Comment

This one works but what happens is it replaces the first occurred image in all the instances of the class .imagePath. So all the image tag has same image. That is the reason why included a function there
0

Use this:

    $('.themeColor').click(function () {
    var getId = $(this).attr('id');
    var imagePath = $('.imagePath');
    if (getId === 'darkTheme') {

        imagePath.attr('src', function (index, attr) {
            if attr {
              return attr.replace('img/', 'imgdark/');
           }
        });
    } else if (getId === 'lightTheme') {
        imagePath.attr('src', function (index, attr) {
            if attr {
                 return attr.replace('imgdark/', 'img/');
}
        });
    }
});

1 Comment

@mdev Thank you so much. This works. I am not even an intermediate and still I cant figure out how that worked. But thanks for your help. It worked with this if block included inside the function. The console error disappeared. Thanks again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.