0

I am just beginning learning HTML/CSS/JS. My experience with programming is only with FileMaker Pro and Databases. I am trying a button that toggles a variable through values ON and OFF. What am I doing wrong?

var content = "ON";

function Change() {

  if (content == 'ON') {
    var content = "OFF";
  } else {
    var content = "ON";
  }
  console.log(content);

}
<!DOCTYPE html>
<html>

<head>
</head>

<body>
  <button onclick="Change();">Change </button>
</body>

</html>
CBroe
  • 91,630
  • 14
  • 92
  • 150
  • 3
    `content = "ON"` is assignment. Use `==` or `===` for equality check. – VLAZ Apr 15 '20 at 07:22
  • 3
    Also, don't use `var` inside the function, you'd be shadowing the outer variable. – VLAZ Apr 15 '20 at 07:23
  • 3
    You shouldn't call the function `Change`. Convention dictates that function names starting with capital letters are reserved for constructor functions / classes. – Quentin Apr 15 '20 at 07:24
  • 2
    Also in case of an on/off switch is will be better to use a boolean instead of a string `contentOn = true` and `contentOn = false`; – Mark Baijens Apr 15 '20 at 07:31
  • Shubham Rawat, please don’t edit people’s questions in a way, that the actual problem they were asking about disappears. I rolled back your edit that replaced the `=` in the comparison part with `==`. While that might be the solution to the problem here, it makes the _question_ lose any meaning if you change it in there. – CBroe Apr 15 '20 at 08:08

0 Answers0