-1

I need help here. In the addEventListener, the keypress is working but the result is always false(default when using switch or else when using if/else statement). I will put my code here so you will understand:

var input = document.getElementById("userAnswer");    
input.addEventListener("keypress", function (enter){
    if(enter.keyCode === 13){
       return startGame(); 
    }
}

function startGame(){
    var userAnswer = document.getElementById("userAnswer");

    //switch statement: always default
    switch(userAnswer){
        case "start":
             codes... "Lets start the game!";
             break;
        default:
             codes... "Looks like you misspelled start! Type start again!"
    }

    //if else statement: always else
    if(userAnswer === "start"){
        codes... "Lets start the game!";
    } else {
        codes... "Looks like you misspelled start! Type start again!"
    }
}

Please help me guys! I'm creating a game app using visual studio, I can't use onclick on input because its js have an anonymous function so I add .addEventListener!

ADDED: In the result, it always says the else or default which is the "looks like you...", even when I type in the input "start" correctly. Are there any errors?

Bonifacio2
  • 3,405
  • 6
  • 34
  • 54
AaronPal
  • 1
  • 3
  • 1
    Where exactly are you returning the value from `keypress` listener? – Teemu Jun 13 '14 at 10:55
  • 1
    ... when `startGame` function seems to return nothing (i.e., `undefined`). – raina77ow Jun 13 '14 at 10:55
  • Please elaborate your question. You're talking about result, but what exactly is the result in the posted code? Notice, that the value returned from an event handler _is not used anywhere_, it goes directly to the bit space, especially it is not going to be set as a value to the said input. – Teemu Jun 13 '14 at 11:23
  • @Teemu I already fixed it, i just need to add .value after the `var userAnswer = document.getElementById("userAnswer");` in the `function startGame()` – AaronPal Jun 13 '14 at 11:32
  • Yes I know, I just have tried to get you improve your question : ). Anyway, isn't it ironic, that you've that comment `//I have an input element` at the first line in your code snippet? – Teemu Jun 13 '14 at 11:42
  • @Teemu I can edit and delete it if you want xD – AaronPal Jun 13 '14 at 11:53
  • It would be appreciated by future visitors, if you edited the question. – Teemu Jun 13 '14 at 11:56
  • @AaronPal Hmm... I didn't mean you should remove the comment, I meant the whole question could be edited to better implement your problem. – Teemu Jun 13 '14 at 12:07
  • @Teemu Just asking, Is there a `prompt()` in Visual Studio? I'm searching it last few days in google but I only found command-prompt Example: `var q1 = prompt("...");` – AaronPal Jun 13 '14 at 12:08
  • Im bad in english. :( – AaronPal Jun 13 '14 at 12:12
  • Sorry, I'm not sure what you mean with `prompt` in VS? The `prompt` in your comment is a native DOM method in browsers though. – Teemu Jun 13 '14 at 12:15
  • Well, you know, I'm creating a game app for windows 8. I have many problems. Search this "random text adventure" and you will know the game that i'm gonna make. There's a little difference about that game and my game. – AaronPal Jun 13 '14 at 12:20

2 Answers2

0

Try this:

var userAnswer = document.getElementById("userAnswer").value;

Since you can only get reference of the element, not the value of it by using getElementById. It will never be some string like "start".

Jack Song
  • 478
  • 4
  • 9
0

You are abusing of "===" operator. You should use "==" to force a type conversion in the comparison. Using "==" you'll get an error because the var userAnswer contains a control and not a string. Then set the value to the variable:

 var userAnswer = document.getElementById("userAnswer").value;

And then use "==" in the if sentence:

if(userAnswer == "start")

You can find a nice explanation of == and === operators here:

Does it matter which equals operator (== vs ===) I use in JavaScript comparisons?

Community
  • 1
  • 1
MarceloRB
  • 86
  • 1
  • 7
  • Comparing different type of variables doesn't cause errors. – Teemu Jun 13 '14 at 11:20
  • I suggest you read the link. If you had used "==" you would understood immediately that the problem was the type mismatch getting the JavaScript error. – MarceloRB Jun 13 '14 at 11:26
  • Yes, the issue is type mismatch, but definitely you can't fix it by using `==` instead of `===`, even `==` would not change an HTMLElement to a string containing the innerHTML of that element... – Teemu Jun 13 '14 at 11:30
  • My opinion is that you should use === only if you know that you can have different types in the comparison. – MarceloRB Jun 13 '14 at 11:33
  • But it's totally irrelevant to the OP's question. `===` can, and almost should be used everywhere. It is never wrong. – Teemu Jun 13 '14 at 11:34
  • Is that basic in javascript? I'm just a beginner here. – AaronPal Jun 13 '14 at 11:50
  • Hi Teemu. What I want to say is that the code is not doing what you expect because you can be allowing that using a generic operator instead of a strict operator. I see two problems in that code: Missing the ".value" and allows compare different types when I just expect strings. – MarceloRB Jun 13 '14 at 11:51
  • AaronPal, I almost never use === and the most code I see use the == operator. I don't think it's a question of basic Javascript, it's about to use what you need. In this case you can use both but is important you know they don't behave the same. – MarceloRB Jun 13 '14 at 11:56
  • When using strict comparing, you don't need to expect anything, everything goes fine every time, automatically, and if it doesn't, you know where the problem lies, unlike when using `==`. – Teemu Jun 13 '14 at 12:08
  • In your code you've allowed comparing a HTML Element with a string. I would not do that in my code. @Teemu, I respect your point of view. – MarceloRB Jun 13 '14 at 12:21