-2

I wanted to write a HTML page with Javascript for a little game. I have many s and every sends a different variable to javascript when clicked. Now the problem with this is, that I wanted to exclude some s for a function, that should apply for every , except the ones excluded.

I wrote it like this:

if (pressedKey != "Leertaste" || "Enter" || "StrgR" || "Alt" || "AltGr" || "Tab" || "ShiftL" || "StrgL" || "ShiftR"){
    document.getElementById(pressedKey).innerHTML = "<img src=\"push1.png\">"
}

But it's not working.. It still triggers the function, although the pressedKey is one of the excluded ones..

I hope someone can help.

3m7ecc

3m7ecc
  • 52
  • 1
  • 10

2 Answers2

2

I think it should be but im not sure, try it please :):

pressedKey != "Leertaste" || pressedKey != "Enter" || pressedKey != "StrgR" || pressedKey != "Alt" || pressedKey != "AltGr" || pressedKey != "Tab" || pressedKey != "ShiftL" || pressedKey != "StrgL" || pressedKey != "ShiftR"

As many of you say i used the wrong seperator, but this one was in the question, so i guessed he needed this. But indeed, he needs:

 pressedKey != "Leertaste" && pressedKey != "Enter" && pressedKey != "StrgR" && pressedKey != "Alt" && pressedKey != "AltGr" && pressedKey != "Tab" && pressedKey != "ShiftL" && pressedKey != "StrgL" && pressedKey != "ShiftR"
Refilon
  • 3,334
  • 1
  • 27
  • 51
  • Oh, ok, I see my mistake.. Thanks! Edit: But it seems like it's still not working.. – 3m7ecc Oct 31 '14 at 13:35
  • 2
    Your expression will always evaluate to true because you compare the same variable to two different values at the same time, and you use the OR operator. For example if you have a var X and the expression `if (X != 1 || X != 2)` even if X == 1 your expression will be true, because X != 2. You should use && instead of || – gabitzish Oct 31 '14 at 13:45
1

@Deer-Outdoor.nl's answer is nearly right (the wrong operator was used), if inelegant (no offense!). You could also put all values in an array and check whether the pressedKey is in that array.

var keys = ["Leertaste", "Enter", "StrgR", "Alt", "AltGr", "Tab", "ShiftL", "StrgL", "ShiftR"];

if (keys.indexOf(pressedKey) === -1) {
    //pressed key is not in array
} else {
    //pressed key is in array
}

Or you could use a switch statement :

switch(pressedKey) {
    case "Leertaste":
    case "Enter":
    case "StrgR":
    //...other cases...
         //pressed key is in those values
        break;
   default:
        //pressed key is NOT in those values
        break;
}

They're pretty much equivalent, it's all a matter of style.

SolarBear
  • 4,534
  • 4
  • 37
  • 53
  • Thanks! It's weird though, that this one worked but @Deer-Outdoor.nl's ones not. – 3m7ecc Oct 31 '14 at 13:44
  • Huh, of course. See @gabitzish's comment on the other answer, the wrong operator was used. I didn't see it either. :) – SolarBear Oct 31 '14 at 13:52
  • 1
    @SolarBear updated the answer. My answer was based on what he thought he needed, so that's why i left the || (or) operator in. Updated my answer tho :) – Refilon Oct 31 '14 at 14:16