ga-wdi-exercises / project1

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

problem with event handler #213

Closed jeffreyeaton76 closed 8 years ago

jeffreyeaton76 commented 8 years ago

I tried to call a method on an object (deck.compare) using an event handler. I expected it would work just as well as calling the method directly deck.compare();. When using the event handler it claims parts of my object method are undefined and this doesn't make sense to me. My repo link is https://github.com/jeffreyeaton76/war and my question is about lines 47 and 48; they are both commented out but when un-commented 47 works and line 48 creates the error.

RobertAKARobin commented 8 years ago

@jeffreyeaton76 I'm going to guess this is because when you use deck.compare in an event handler this changes. Inside an event callback, this becomes the element on which the event was fired. So if I have:

document.getElementById("button").addEventListener("click", deck.compare);

...this would be the button.

jeffreyeaton76 commented 8 years ago

That was totally it! Thanks!