1

This is my code, which I got from this site, which is confirmed as working. Not for me. Don't mind the div tag.

enter image description here

Paths are good, it's showing images in DW, but nothing happens after clicking on image on website...

Also, heres the code:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<!-- Behavioral Meta Data -->
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<!-- Styles -->
<link rel="stylesheet" type="text/css" href="Style.css"/>
<title>Untitled Document</title>

</head>

<body>

  <div id="container" class="container">
    <ul id="scene" class="scene border fill">
      <li id="Par_3" class="layer expand-width" data-depth="0.25"><img src="../Img/Par_3.svg" alt="Gallery"></li>
      <li id="Par_2" class="layer expand-width" data-depth="0.20"><img src="../Img/Par_2.svg" alt="Grass_1"></li>
      <li id="Par_25" class="layer expand-width" data-depth="0.20"><img src="../Img/Par_2.5.svg" alt="About me"></li>
      <li id="Par_1" class="layer expand-width" data-depth="0.15"><img src="../Img/Par_1.svg" alt="Grass_2"></li>
      <li id="Par_15" class="layer expand-width" data-depth="0.15"><img src="../Img/Par_1.5.svg" alt="Contact"></li>
      <li id="Par_0" class="layer expand-width" data-depth="0.10"><img src="../Img/Par_0.svg" alt="Rock"></li>
      <li id="Logo" class="layer expand-width"  data-depth="0.05"><img src="../Img/Logo.svg" alt="Logo"></li>
    </ul>

    <img alt="" src="../Img/Click_u.svg" id="cm" onclick="changeImage()"  />

  </div>

  <!-- Scripts -->

  <script language="javascript">
    function changeImage() {

        if (document.getElementById("cm").src == "../Img/Click_u.svg") 
        {
            document.getElementById("cm").src = "../Img/Click_ch.svg";
        }
        else 
        {
            document.getElementById("cm").src = "../Img/Click_u.svg";
        }
    }
</script>

  <script src="Parallax.js"></script>
  <script>

  var scene = document.getElementById('scene');
  var parallax = new Parallax(scene);

  </script>

</body>
</html>
4
  • 2
    Please post your code in text not pictures. Commented Feb 23, 2017 at 17:21
  • what site? there's a missing link. also please dont add code as image. Commented Feb 23, 2017 at 17:21
  • 1
    do you have any errors in your console? Commented Feb 23, 2017 at 17:22
  • console.log(document.getElementById("cm").src) Commented Feb 23, 2017 at 17:23

1 Answer 1

3

The thing is, .src returns not only the relative path (../Img/Click_u.svg) but the entire path (http://www.yourwebsite.com/images/Img/Click_u.svg).

So that if condition will never be true, and it will never change the image.

Example screenshot

Perhaps try something along the lines of

if (document.getElementById("cm").src.indexOf("Click_u.svg") != -1)

To check if the snippet "Click_u.svg" can be found inside the larger string src.

Or use

document.getElementById("cm").getAttribute("src")`

and it will return you the exact value of the attribute (which is the relative path)


The code that should work on your case:

if (document.getElementById("cm").getAttribute("src") == "../Img/Click_u.svg")
{
    document.getElementById("cm").src = "../Img/Click_ch.svg";
}
else 
{
    document.getElementById("cm").src = "../Img/Click_u.svg";
}

For some weird reason, there seems to be an invisible character just at the end of the getAttribute method. No clue as to why, but if you, reader, are willing to use this code, please retype it instead of copy-pasting.

Error 1

Error 2

That's a bug for another time

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

16 Comments

any way to set this to use relative path ?
As far as I know, src won't ever return the relative path :\ I could be wrong though EDIT: Nevermind! I just thought of a way haha. Using .getAttribute("src"), you can fetch the relative path
Should it be something like this ? Because its still not working for me... <img alt="" src="../Img/Click_u.svg" id="cm" onclick="changeImage()" /> </div> <!-- Scripts --> <script language="javascript"> function changeImage() { if (document.document.getElementById("cm").getAttribute("../Img/Click_u.svg"); { document.document.getElementById("cm").getAttribute("../Img/Click_ch.svg"); } else { document.document.getElementById("cm").getAttribute("../Img/Click_u.svg"); } }
You're using document.document there. And to set the source, you still use .src = "...", but to get it, you can use the .getAttribute(). I'll edit the answer to include some code that might work.
Try to do what epascarello said and console.log(document.getElementById("cm").getAttribute("src"));, then check the console to see what it gives you.
|

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.