user4523269
user4523269

Reputation:

if else not working js

Here is my Code i just want to change x value on click if it is x then change y ..if it is y then change to x again but after become y value cant change to x .. the control is always on 3rd section .. whats wrong

jQuery(document).ready(function(){
           var x;
          $(".ticktack tr td").click(function(){
          if(x == null){

          alert('section 1' );
            x = 'X';
           }
          else if(x == 'X'){
          alert('section 2');
             x = 'Y';
           }
          else if(x == 'Y'){
          alert('section 3');
                x == 'X';
           }
            $(this).html(x);
          })

        }); 

Upvotes: 1

Views: 80

Answers (7)

Hardeep Randhawa
Hardeep Randhawa

Reputation: 47

    Used assignment operator instead of comparison operator in Third section
    try this code.

    jQuery(document).ready(function(){
       var x;
      $(".ticktack tr td").click(function(){
      if(x == null){

      alert('section 1' );
        x = 'X';
       }
      else if(x == 'X'){
      alert('section 2');
         x = 'Y';
       }
      else if(x == 'Y'){
      alert('section 3');
            x = 'X';
       }
        $(this).html(x);
      })

    }); 

Upvotes: 0

Pratik Joshi
Pratik Joshi

Reputation: 11693

Change x == 'X'; to x = 'X' as you have to assign a value x=='X' does Nothing. If you put comparions Or ternary operator like x =='X'?If true then do this:else this; in if then it gives you true or false result

Upvotes: 0

Shreejibawa
Shreejibawa

Reputation: 1868

Well you are using comparison operator after alert('Section 3'). Instead you must use assignment operator! Try this one:

jQuery(document).ready(function(){
  var x;
  $(".ticktack tr td").click(function(){
  if(x == null){
    alert('section 1' );
    x = 'X';
  } else if(x == 'X') {
    alert('section 2');
    x = 'Y';
  } else if(x == 'Y') {
    alert('section 3');
    x = 'X';
  }
  $(this).html(x);
  });
}); 

Upvotes: 0

coderz
coderz

Reputation: 4999

You added one more = in the third section. Try:

jQuery(document).ready(function(){
               var x;
              $(".ticktack tr td").click(function(){
              if(x == null){

              alert('section 1' );
                x = 'X';
               }
              else if(x == 'X'){
              alert('section 2');
                 x = 'Y';
               }
              else if(x == 'Y'){
              alert('section 3');
                    x = 'X';
               }
                $(this).html(x);
              });

            });

Upvotes: 0

Cyrille Armanger
Cyrille Armanger

Reputation: 759

Actually you have two mistakes, one ; missing and x == X which should x = X

$(document).ready(function(){
           var x;
          $(".ticktack tr td").click(function(){
              if(x == null){
                    alert('section 1' );
                    x = 'X';
               } else if(x == 'X'){
                    alert('section 2');
                    x = 'Y';
               } else if(x == 'Y'){
                    alert('section 3');
                    x = 'X';
               }
            $(this).html(x);
          });

        }); 

Upvotes: 1

Adrien
Adrien

Reputation: 1947

You have an extra '=' in the last else if statement!

x = 'X'; should work :)

Upvotes: 0

Paul Draper
Paul Draper

Reputation: 83421

alert('section 3');
x == 'X';

should be

alert('section 3');
x = 'X';

JSFiddle

Upvotes: 0

Related Questions