Caststupider
Caststupider

Reputation: 53

Function works only once

My function works only once when I click on blue, if I want to make it black again, it no longer works.

What's the problem? Is there an easier way to make that?

I know it works, but not in this way. I would take the value to the next page, and I thought its the simplest way.

$(document).ready(function(){
    var x;

    $("#dark").click(function(){
        dark(x);
        entscheid (x);
        }); 

    $("#blue").click(function(){
        blue(x);
        entscheid (x);
        }); 

    function blue(x){
        var x=0;    
    }

    function dark(x){
        var x=1;    
    }

    function entscheid(x){
        if(x==1){
          $("nav").attr("class","navigation-bar dark fixed-top");
          $(".dropdown-menu").attr("class","dropdown-menu dark");
          $(".heading").attr("class", "heading bg-dark fg-white");
          $(".container-middleBlue, .container-middleDark ").attr("class","container-middleDark");
        }
        else{
          $("nav").attr("class","navigation-bar blue fixed-top");
          $(".dropdown-menu").attr("class","dropdown-menu blue");
          $(".heading").attr("class", "heading bg-blue fg-white");
          $(".container-middleDark, .container-middleBlue").attr("class","container-middleBlue");
        }
    }
    })

Upvotes: 0

Views: 77

Answers (1)

jfriend00
jfriend00

Reputation: 707158

These functions:

function blue(x){
    var x=0;    
}

function dark(x){
    var x=1;    
}

are creating and modifying a local variable, not your higher scoped variable x. Because you have var in front of x, it is declaring a new variable that is local only to this function. Thus, they don't change the higher scoped variable that you are passing when you call entscheid (x);


I don't quite understand why you're using this other variable at all as you can just pass the desired value to your entscheid function and not use the global at all.

$("#dark").click(function(){
    entscheid(1);
}); 

$("#blue").click(function(){
    entscheid(0);
}); 

Upvotes: 3

Related Questions