2

I'm newbie at javascript. I want to know what I'm doing wrong. I want to make image src swap on function onclick. After first click my image change to "close" but everytime more it seems to be closed. Just in case, I don't want other solution. I just want to know what is going wrong on my script. Thanks!

function menuOpenClose()
{
    var a = document.getElementById("mobile-menu-icon")

    if (a.src === "icon/menu.svg") {
        a.src = "icon/close.svg";
    } else {
        a.src = "icon/menu.svg";
    }
}
img {
        display: block;
        width: 32px;
        height: 32px;
    }
<div class="menu-icon" onclick="menuOpenClose();">
  <img id="mobile-menu-icon" src="icon/menu.svg">
</div>

2
  • In your if statement you are not comparing but assigning. Use a double or triple equal sign to compare the variable with the string. Commented Feb 7, 2019 at 19:59
  • I've removed the syntax mistake that's acting as a red herring here. New answers should address the issue with a.src. Commented Feb 7, 2019 at 20:07

4 Answers 4

1

You can do it in two ways (indexOf() or getAttribute()):

if (a.src.indexOf("icon/menu.svg") != -1) {

or:

if (a.getAttribute('src') == "icon/menu.svg") {

The snippet:

function menuOpenClose()
{
    var a = document.getElementById("mobile-menu-icon")

    //if (a.getAttribute('src').indexOf("icon/menu.svg") != -1) {
    if (a.getAttribute('src') == "icon/menu.svg") {
        a.src = "icon/close.svg";
    } else {
        a.src = "icon/menu.svg";
    }
    console.log(a.src);
}
<div class="menu-icon" onclick="menuOpenClose();">
    <img id="mobile-menu-icon" src="icon/menu.svg">
</div>

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

Comments

1

Your condition in the if statement should be like below

   if (a.src === "icon/menu.svg") {

Comments

0

You are using an assignment operator in your if statement instead of a comparison operator.

Change this:

if (a.src = "icon/menu.svg")

To this:

if (a.src === "icon/menu.svg")

Check and run the following Code Snippet for a practical example of the above approach:

/* CSS */
img {display: block;width: 32px;height: 32px;}
<!-- JavaScript -->
<script>
function menuOpenClose(){
    var a = document.getElementById("mobile-menu-icon")

    if (a.src === "https://picsum.photos/50/50?image=0") {
        a.src = "https://picsum.photos/50/50?image=1";
    } else {
        a.src = "https://picsum.photos/50/50?image=0";
    }
}
</script>

<!-- HTML -->
<div class="menu-icon" onclick="menuOpenClose();">
  <img id="mobile-menu-icon" src="https://picsum.photos/50/50?image=0">
</div>

2 Comments

If I change it my image didn't change even in first click.
@ownidea Check the demo Code Snippet I added in the answer using your exact code but with the proper operator === used.
0

First of all change = to === it a different:

a.src === "icon/menu.svg" this correct

Second as you can see you never will change you image because your condition can't catch it.

this is anwer from console:

@>src https://stacksnippets.net/icon/menu.svg

you always do else

        function menuOpenClose() {
            var a = document.getElementById("mobile-menu-icon")

            console.log(`@>src`,a.src)

            if (a.src === "icon/menu.svg") {
                a.src = "icon/close.svg";
            } else {
                a.src = "icon/menu.svg";
            }
        }
<div class="menu-icon" onclick="menuOpenClose();">
    <img id="mobile-menu-icon" src="icon/menu.svg">
</div>

Comments

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.