-2

The button that I'm working on, when clicked, pans a div to the left. In the script, the "if" condition works fine but the "if else" condition doesn't.

Did I forgot something to include in the script?

here's my script:

<script>
    function panleft(el){
        var elem = document.getElementById(el);

        if (elem.style.left = "90%"){   
            elem.style.transition = "left 0.3s ease-in 0s";
            elem.style.left = "70%";
            }

        else if (elem.style.left = "70%"){  
            elem.style.transition = "left 0.3s ease-in 0s";
            elem.style.left = "90%"
            }

    }

</script>
2
  • 1
    What is difference between = and == and ===? Commented Feb 20, 2016 at 14:09
  • Have you heard of JSHint and JSLint? If yes/no, use it. Commented Feb 20, 2016 at 14:10

1 Answer 1

0

In Javascript, the equal (=) operators work as follows:

  • x = y is assignation (i.e. give variable X value Y)
  • x == y is a loose equality test (i.e. is x equal to y), it's loose because it does type coercion (changes types to try and find a match), such as if "1" == 1 (string vs number), Javascript will convert one to the other format until it finds a match or has failed to do so.
  • x === y is strict equality. (is x equal to y) WITHOUT changing primitive type.

In your example you're assigning values, not testing equality. That's the source of your problems.

Also, your "if" statement is not truly working, since you're also making an assignation, not an equality test.

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

1 Comment

I got it now, thank you so much!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.