-3

Does anyone know what typo I made, because my editor, and I can't seem to make it work

number=5;
switch (number) {
    case 5:
        box.onclick = function1;
        break;
    case 50:
        box.onclick = function2;
        break;
}

I've also tried doing it in switch.

  • 2
    Are you actually using a var named "var"? – adv12 May 05 '14 at 19:47
  • 1
    Var - generic for my variable name, and I don't want to give away what exactly I'm working on. –  May 05 '14 at 19:50
  • 1
    For sake of clarity, you'd better call it something else, because right now that's the most obvious problem with your code sample and what all your answers are going to be about. – adv12 May 05 '14 at 19:51
  • Why don't you like placeholders? –  May 05 '14 at 19:54
  • 2
    As all your answers so far have pointed out, "var" is a really bad name to choose for a placeholder as it is a keyword in JavaScript. Pick "x", pick "y", it doesn't matter--just not the keyword "var". – adv12 May 05 '14 at 19:57
  • So, now that you've cleared that up, what are you expecting to happen that's not happening? – adv12 May 05 '14 at 20:00
  • That's why I chose what I did for a user alias –  May 05 '14 at 20:00
  • The first event is getting triggered, but when I choose the 2nd one it doesn't seem to trigger. Both functions work when I test them, but only the first one does when I run it in the if statement. –  May 05 '14 at 20:01
  • 1
    So you mean that when you set `number` to 50, then clicking the box doesn't cause function2 to be called? – adv12 May 05 '14 at 20:03
  • I rolled back the question again based on the comments above that state that the use of `var` was just for the purpose of hiding what the original poster is working on, and is not actually the cause of the problems that he or she is facing. I won't touch it again, sorry. –  May 11 '14 at 04:04

4 Answers4

3

Try not using the reserved word var as a variable name:

var x=5;
    if (x==5){
        box.onclick = function1;
    }
    if(x==50){
        box.onclick = function2;
    }
Thomas Junk
  • 5,588
  • 2
  • 30
  • 43
2

var is the reserved word to create variables. You can't use it as a name. This is the correct syntax:

var x = 5;

if (x == 5) {
    box.onclick = function1;
}

if (x == 50) {
    box.onclick = function2;
}
salezica
  • 74,081
  • 25
  • 105
  • 166
1

You can't use reserved JavaScript words for declaring variables.

Try to replace var=5 to myVar=5 for example.

var myVar = 5;

if (myVar ==5){
    box.onclick = function1;
}
if(myVar ==50){
    box.onclick = function2;
}

Also, check out this reference: JavaScript Reserved Words

Artem Petrosian
  • 2,964
  • 1
  • 22
  • 30
1

With this code:

number=5;
switch (number) {
    case 5:
        box.onclick = function1;
        break;
    case 50:
        box.onclick = function2;
        break;
}

case 50 will never be hit because you set number to 5 before entering the switch block. Therefore box onclick will never equal function2. Therefore, when you click the box, function2 will not be run. Is this really an accurate representation of your actual code, or is it a simplification that has left out important information?

adv12
  • 8,443
  • 2
  • 24
  • 48
  • Actually function 1 makes it 50, function 2 makes it 5. – a coder May 05 '14 at 20:36
  • Okay, but is the switch code re-run after number is changed, or is it only run once, at startup? The switch code would have to be re-run in order for `box.onclick` to get set to function2. – adv12 May 05 '14 at 20:38
  • I have it at the point where the function will stop when you click the "button" – a coder May 05 '14 at 20:43
  • Also, it would help other answerers a lot if you added this comment to your question description. Maybe even show a simplified function1 and function2 showing how they modify number. – adv12 May 05 '14 at 20:43
  • I know both functions work independently, and together, but I'm trying to write them into stopping when they reach point A, or point B Lets say function 1 sets number to 50, and function 2 sets number to 5. – a coder May 05 '14 at 20:48