Function call inside click event not working in javascript

Viewed 33

I'm a newbie to javascript. I am trying to call a pre-defined function inside the click event which does not seem to work. Please help.

const optionsList = ["dashboard", "accounts", "expenses", "income", "budgets", "reports"];

function goTo(classname){
  optionsList.forEach(function(item){
    if(classname === item){
        $("." + item).css("display", "block");
        $("#" + item).addClass("active");
      } else{
        $("." + item).css("display", "none");
        $("#" + item).removeClass("active");
      }
  });
}

goTo("dashboard");

$(".navbr").click(function(evt){
  goTo(evt.target.id);    //This function call is working
});

$("#accountsBtn").click(function(){
  goTo("accounts");   //This function call is not working
});

The function call is working the first time I call the goTo function, but not the second time. I wonder why?

Also, if I insert a sample alert inside the click event, the alert is firing but the goTo("accounts") is not....

$("#accountsBtn").click(function(){
  alert("I got clicked");  //This line is firing
  goTo("accounts");   //This function call is not working
});
0 Answers
Related