0
<html>
<body>
    <input type="text" value="0"    id="x1" onblur="ThisEvent()"/>x1</br>               
    <input type="text" value="0"    id="x2">x2      </br>

<script>
    var x1      = document.getElementById("x1");    
    var x2      = document.getElementById("x2");

function ThisEvent(){
    if (x1=1)   {x2.value--;}else{
    if (x1=2)   {x2.value++;}else{
    if (x1=3)   {x2.value+0;}}}
    }
</script>
</body>
</html>

seems to only fire the first if statement and ignore the else situation. any help would greatly be appreciated

2 Answers2

1

= is an assignment operator.

== & === are comparison operators. (See this question for the difference between them)

It's stopping on the first if statement because x1=1 returns 1, which is truthy.

Once you switch to using comparison operators, you should be using x1.value == 1 to compare the numerical value of the <input>, or you could use x1.value === "1".

Community
  • 1
  • 1
4castle
  • 32,613
  • 11
  • 69
  • 106
0
<html>
<body>
    <input type="text" value="0"id="x1" onblur="ThisEvent()">x1     </br>   
    <input type="text" value="0"    id="x2"     >   x2      </br>
<script>
    var x1      = document.getElementById("x1");    
    var x2      = document.getElementById("x2");

function ThisEvent(){// needs a lot of work done to it
    if (x1.value==1)    {x2.value--;}else{
    if (x1.value==2)    {x2.value++;}else{
    if (x1.value==3)    {x2.value+0;}}}}
</script>
</body>
</html>