ga-wdi-exercises / project1

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

understanding logic behind event listener. #129

Closed phantomhaircuts closed 9 years ago

phantomhaircuts commented 9 years ago

Talked to Andy about this earlier, made some changes in the function, but still having trouble figuring out what I need to change from here.

It seems like no matter what I do, its logging more than I anticipate...

function checkAnswer(){ $(correctIndex[question]).on("click", function right (){ console.log("score!") console.log(correctIndex[question]); alert("correct!") score = score + 1000; $("#score").text(score) $(correctIndex[question]).off("click", right); })

$(wrongIndex[question]).on("click", function wrong (){ console.log("wronganswer"); console.log("wrongIndex[question]") alert("Wrong!") score = score - 1000; $("#score").text(score) $(wrongIndex[question]).off("click", wrong); }) }

jshawl commented 9 years ago

I recommend taking the event listeners outside of your checkAnswer function. You should listen for clicks on each button when the dom loads, and then check to see if it's right or wrong.