-1

I have created a switch in my website. It works like a light switch. When people click on it, the background color and the color of some text paragraph will switch to different colors. I wrote the code below and successfully did the task:

  $(".clickme").click(function () {      
     $('html').css('background-color', 'black');
     $('#top_nav a').css('color', '#ff7b00');
     $('.back_face').css('background-color', 'black');
     return false;
  });

But now I want to turn the light back on. I have created a var called lighton, and set it to 0. And I tried to use an if else statement to do the task, but it doesn't work. Below is the code I wrote and failed:

<a href="" class="clickme">Click me</a>

<script>    
  $(".clickme").click(function () {    
  var lightoff = 0;   
  if (lightoff=0){    
      $('html').css('background-color', 'black');
      $('#top_nav a').css('color', '#ff7b00');
      $('.back_face').css('background-color', 'black');
      lightoff = 1;
      return false;    
 }else{

      $('html').css('background-color', 'white');
      $('#top_nav a').css('color', 'black');
      $('.back_face').css('background-color', 'white');
      lightoff = 0;
      return false;
   }
});

</script>

I just started learning JavaScript recently, and I have checked the JavaScript Conditional Statements syntax. So I hope somebody knows what's wrong with my code. Thank you so much.

1
  • 1
    put var lightoff = 0; out of your click() event Commented Aug 2, 2013 at 6:00

5 Answers 5

5

You need to use if (lightoff==0){.

= is an assignment operator whereas == is a comparison operator.

When using an assignment operator within a condition, the assigned value will be evaluated, so (lightoff=0) will always evaluate to 0 which is then casted into false. The most common case for using an assignment operator within an conditional is when the return value of a function is being assigned, e.g. ( lightoff = checkIfLightIsOff() )

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

4 Comments

Good point Sekharan. Javascript is funky with all the truthy and falsy 'values'. Originally I was going to say for simple examples it's safe for the double equals, but considering the nature of JS I'd start using the equality operator from the get go. Also asifrc, jQuery is a good way to learn the basics, but you'll need to know what's going on under the hood at some point. Start now!
@Phix I had used pure javascript for over 5 years before ever hearing of jquery. I'm not offended or anything lol, but just curious: is there anything I wrote that prompted you to recommend that I learn more about javascript (or that I lean too heavily on jQuery)?
My bad, it was directed at the OP. Didn't scroll up high enough on my phone. My mistake!
That's kind of what I thought lol. But I do absolutely agree: @Hold_My_Anger, given the luxury of time, you should most definitely "rough it" and use pure javascript for a bit before jumping into jQuery. That'll help you clientside scripting in general, and is also helpful when you try out something like Node.js
3

Since you want to use the value of lightoff across multiple calls to the callback method, it should be defined out the scope of the handler method, so that multiple calls to the method will share the same instance of lightoff

Also as @zzlalani pointed out the = operator stands for assignment, what you need is the comparator operator which is ==

it should be

var lightoff = 0;

$(".clickme").click(function () {

    if (lightoff==0){

        $('html').css('background-color', 'black');
        $('#top_nav a').css('color', '#ff7b00');
        $('.back_face').css('background-color', 'black');
        lightoff = 1;
        return false;

    }else{

        $('html').css('background-color', 'white');
        $('#top_nav a').css('color', 'black');
        $('.back_face').css('background-color', 'white');
        lightoff = 0;
        return false;
    }
});

1 Comment

the condition should be if (lightoff==0){ ratherthan of if (lightoff=0){ you need to use == two equaltos
1

I'm not a Javascript expert, but it looks like you used '=' instead of "==", which would set the variable to 0 instead of comparing it.

Comments

0

instead of = you should used == or ===:

  • = is assignment operator

  • == equal to ie,
    x==8 false
    x==5 true

  • === exactly equal to (equal value and equal type) ie,
    x==="5" false
    x===5 true

some other cases:

'' == '0'           // false
0 == ''             // true
0 == '0'            // true

false == 'false'    // false
false == '0'        // true

false == undefined  // false
false == null       // false
null == undefined   // true

Comments

0

An alternative approach is the toggle event

$(".clickme").toggle(function () {    
    $('html').css('background-color', 'black');
    $('#top_nav a').css('color', '#ff7b00');
    $('.back_face').css('background-color', 'black');
    return false;    
}, function() {
    $('html').css('background-color', 'white');
    $('#top_nav a').css('color', 'black');
    $('.back_face').css('background-color', 'white');
    return false;
});

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.