ga-wdi-exercises / project1

[project] GA- Project 1
3 stars 75 forks source link

trouble getting keypresses to work #263

Closed megancoyle closed 8 years ago

megancoyle commented 8 years ago

At the moment, my game is controlled by users clicking buttons. I want to add the functionality that when they press "Enter" or the right arrow key, the users are also able to move to the next card.

In my script.js file here: https://github.com/megancoyle/flash-cards

I've tried to add in this functionality with this:

var keypressNext = $("body").keypress(function (e) { if (e.which == '13' || e.which == '39') { guessAnswer(e); } });

For some reason, no matter what I try, I keep getting this error:

script.js:53 Uncaught TypeError: Cannot read property 'toLowerCase' of undefined

It works for the first card that displays, but breaks after that.

jshawl commented 8 years ago
var keypressNext = $("body").keypress(function (e) { 
  if (e.which == '13' || e.which == '39') { 
    console.log(e) //guessAnswer(e); 
  } 
});

what is e there?

megancoyle commented 8 years ago

It's referring to the keypress event, I found that solution here: http://stackoverflow.com/questions/17678161/how-to-trigger-enter-event-with-jquery

It was the only one where I was somewhat able to get a change with moving to the next card.

jshawl commented 8 years ago

doesn't guessAnswer() take a letter and not an event?

megancoyle commented 8 years ago

True, I guess I was thinking of preventing the default action of submitting something, but I'm realizing there isn't a form involved so that isn't necessary. When I get rid of the e for guessAnswer, and just try to call the function, I still get that typeerror.

I think I'm doing something wrong with this when I call the guessAnswer function, but I can't quite figure it out: var $userGuess = $guessInput.val().toLowerCase();

jshawl commented 8 years ago

I get script.js:45 Uncaught ReferenceError: keypressNext is not defined with the code that's on github now