简体   繁体   中英

Why won't a simple Javascript if condition execute?

<!DOCTYPE html>
<html>
<body>

<button onClick="Display()"></button>

<script>
function Display() {
    var a = 0;
    if(a = 0){
      alert("hello world");
    }
} 

</script>

</body>
</html>

I run the page and clicked the button and nothing happens. I can't for the life of me figure out why...

Edit: on another note, THIS code executes no matter what, even if I haven't defined var CorrectActivities:

function Display() {

    if(CorrectActivities = 3) {
        document.getElementById("ActivitiesResult").innerHTML = '<span>style="color:DarkGreen;font-weight:bold;font-size:30px;">3 out of 3 correct!</span>';

    } else if (CorrectActivities = 2) {
        document.getElementById("ActivitiesResult").innerHTML = '<span style="color:Crimson;font-weight:bold;font-size:30px;">2 out of 3 correct!</span>';

    } else if (CorrectActivities = 1) {
        document.getElementById("ActivitiesResult").innerHTML = '<span style="color:Crimson;font-weight:bold;font-size:30px;">1 out of 3 correct!</span>';

    } else {
    document.getElementById("ActivitiesResult").innerHTML = '<span style="color:Crimson;font-weight:bold;font-size:30px;">0 out of 3 correct!</span>';
    }
}

Edit2: thanks for the answers. First code is fixed, second is still broken regardless of what I try. Going to look for errors elsewhere in the script...

When you compare values, you should use == (equal) or === (equal and same type). = is only for setting variable value.

 <!DOCTYPE html> <html> <body> <button onClick="Display()"></button> <script> function Display() { var a = 0; if(a == 0){ alert("hello world"); } } </script> </body> </html> 

To your another code, I created a jsfiddle . There you have same problem as in first part, wrong comparison operator. It didn't run if CorrectActivities wasn't declared. I fixed it (like in first part), declared variable and added three buttons to test all cases and it seems to work.

You also have small typo in case 3, you have an extra closing bracket at <span>style

a = 0 is assignment not a valid condition. a == 0 , a === 0 or !a will all work for what you want.

Same is the case with CorrectActivities , use CorrectActivities == 3 instead of CorrectActivities = 3 .

in javascript = assign a value and return it, == or === compare values

The first part wont execute because:

  • You click the button
  • Display get called
  • if(a = 0) evaluate to if(0) which is to javascript if(false)

so the code inside if won't run. what you want is if(a == 0) :

 function Display() { var a = 0; if (a == 0) { alert("hello world"); } } 
 <button onClick="Display()"></button> 

for the second part if(CorrectActivities = 3) will be if(3) which is to javascript if(true) so it will always run. again what you want is using the == operator:

function Display () {
  if ((CorrectActivities == 3)) {
    document.getElementById('ActivitiesResult').innerHTML = '<span>style="color:DarkGreen;font-weight:bold;font-size:30px;">3 out of 3 correct!</span>'
  } else if ((CorrectActivities == 2)) {
    document.getElementById('ActivitiesResult').innerHTML = '<span style="color:Crimson;font-weight:bold;font-size:30px;">2 out of 3 correct!</span>'
  } else if ((CorrectActivities == 1)) {
    document.getElementById('ActivitiesResult').innerHTML = '<span style="color:Crimson;font-weight:bold;font-size:30px;">1 out of 3 correct!</span>'
  } else {
    document.getElementById('ActivitiesResult').innerHTML = '<span style="color:Crimson;font-weight:bold;font-size:30px;">0 out of 3 correct!</span>'
  }
}

This is because instead of comparing,you are assigning it.

So the statement if(a = 0) always comes out to be true.

Use == instead of =.

function Display() {
    var a = 0;
    if(a = 0){
        alert("hello world");
    }
} 

This will help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM