0

This code meets all conditions, someone please tell me where i am going wrong; I have checked and i cant see anything wrong with the code. why are all the conditions met.

<script>
$( document ).ready(function() {
    var style = $('#el_teams_TeamFormation span').html(); 
    alert (style);
         if (style = "4-4-2")
          { 
            document.getElementById('footballpitch1').style.display = "none";
            document.getElementById('footballpitch2').style.display = "none";
            document.getElementById('footballpitch3').style.display = "none";
            document.getElementById('footballpitch4').style.display = "none";
            document.getElementById('footballpitch1').style.display = "block";
         }
         if (style = "3-4-3")
          {
            document.getElementById('footballpitch1').style.display = "none";
            document.getElementById('footballpitch2').style.display = "none";
            document.getElementById('footballpitch3').style.display = "none";
            document.getElementById('footballpitch4').style.display = "none";
            document.getElementById('footballpitch2').style.display = "block";
         } 
         if (style = "3-5-2") {
            document.getElementById('footballpitch1').style.display = "none";
            document.getElementById('footballpitch2').style.display = "none";
            document.getElementById('footballpitch3').style.display = "none";
            document.getElementById('footballpitch4').style.display = "none";
            document.getElementById('footballpitch3').style.display = "block";
         }
         if (style = "4-5-1") {
            document.getElementById('footballpitch1').style.display = "none";
            document.getElementById('footballpitch2').style.display = "none";
            document.getElementById('footballpitch3').style.display = "none";
            document.getElementById('footballpitch4').style.display = "none";
            document.getElementById('footballpitch4').style.display = "block";
         }
});

1
  • 6
    Nothing wrong? Using assignment operator instead of a comparison operator in if's condition is not wrong?
    – Teemu
    Apr 3, 2014 at 18:56

2 Answers 2

3

It's worth familiarising yourself with the different kinds of comparison operators:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comparison_Operators

Put simply, single (=), double (==) and triple (===) equals signs each mean different things to JavaScript.

A single = is an assignment operator, so in each of your if statements you're essentially asking the script to assign a value of "4-4-2" (and so on) to the variable style instead of comparing the two values.

If you use == (with type coercion) or === (no coercion) your code should work fine.

2
  • Something is wrong, when i put == , the codes are not executed meaning no condition is met. Apr 3, 2014 at 20:13
  • @user3395611 Are you sure everything else in your code is set up correctly? I set up a Fiddle based on your code and it's working as expected: jsfiddle.net/QD334 Apr 4, 2014 at 8:10
1
if (style == "4-4-2")
          { 
            document.getElementById('footballpitch1').style.display = "none";
            document.getElementById('footballpitch2').style.display = "none";
            document.getElementById('footballpitch3').style.display = "none";
            document.getElementById('footballpitch4').style.display = "none";
            document.getElementById('footballpitch1').style.display = "block";
         }
         if (style == "3-4-3")
          {
            document.getElementById('footballpitch1').style.display = "none";
            document.getElementById('footballpitch2').style.display = "none";
            document.getElementById('footballpitch3').style.display = "none";
            document.getElementById('footballpitch4').style.display = "none";
            document.getElementById('footballpitch2').style.display = "block";
         } 
         if (style == "3-5-2") {
            document.getElementById('footballpitch1').style.display = "none";
            document.getElementById('footballpitch2').style.display = "none";
            document.getElementById('footballpitch3').style.display = "none";
            document.getElementById('footballpitch4').style.display = "none";
            document.getElementById('footballpitch3').style.display = "block";
         }
         if (style == "4-5-1") {
            document.getElementById('footballpitch1').style.display = "none";
            document.getElementById('footballpitch2').style.display = "none";
            document.getElementById('footballpitch3').style.display = "none";
            document.getElementById('footballpitch4').style.display = "none";
            document.getElementById('footballpitch4').style.display = "block";
         }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.