1

I'm having trouble with an IF statement that checks a DOM element and replaces the current src with a new src. In the code below, I know the source for sceneImageID is the image after the ==, yet the IF statement does not trigger.

var scene = document.getElementById('sceneImageID').src;
        console.log("scene before if: " + scene);
        if (scene.endsWith == "Media/Town/Town_bw.png") {
            scene = "Media/Town/Town_bw_noscrap.png";
            console.log("inside if statement: " + document.getElementById("sceneImageID").src);
        }

EDITS: added the .endsWith piece, but I'm having the same issue of it not entering the IF statement.

The first console returns: http://34.68.254.120:8080/Media/Town/Town_bw.png

Thanks in advance.

4
  • 2
    Are you sure that is what it is? debug it: console.log(document.getElementById('sceneImageID').src); Commented Dec 23, 2022 at 20:21
  • 1
    You didn't give any context, but... Do what epascarello said to see if it outputs "Media/Town/Town_bw.png". Make sure you referenced the id correctly (which above check also confirms). Perhaps you are trying to access the image before it has finished loading? Commented Dec 23, 2022 at 20:27
  • Yes, sorry, I do have a console log and it returns the following. Have edited the question: 34.68.254.120:8080/Media/Town/Town_bw.png Commented Dec 23, 2022 at 20:40
  • 1
    Because the src adds the protocol and domain of the page. So it will never match. Commented Dec 23, 2022 at 20:43

2 Answers 2

2

Relative sources will get expanded to a full URL, so consider checking for ending with instead.

if (document.getElementById('sceneImageID').src.endsWith("Media/Town/Town_bw.png")) {
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, this seemed like the exact solution, but I don't have it working. See the edits to the question.
@SleepyWakes You wrote it wrong. Look at the code in my answer. endsWith is a function, so you call it as .endsWith("Media/Town/Town_bw.png"). You could've directly copied it from my answer.
0

Just to make sure that you are setting the image ID:

<img id="ImgTown" src="Media/Town/Town_bw.png">

<script>
var checkImage = document.getElementById("ImgTown");
if (checkImage.src == "Media/Town/Town_bw.png") 
{
    checkImage.setAttribute("src", "Media/Town/Town_bw_noscrap.png");
}
</script>

This script will just be triggered if you load the page with the Media/Town/Town_bw.png as the image source. Else, you must have an event like "onClick" calling this using a function.

1 Comment

You're absolutely right, after I fixed the main IF problem I realized I wasn't actually setting the new element src. Thanks for the heads up.

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.