Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

jQuery click event help

Hi, I am working on a project and I need to use some jQuery to light up a button when it is clicked. The code that I have lights up the buttons when they are clicked, but it takes 2 clicks for it to work when you are trying it for the first time. After that they light up every time. If someone could take a look and help me get whats going on I would appreciate it. The function is being called on line 95, and I am using a method on it from line 11. Also, the game will need to be turned on first, and then the start button needs to be pressed in order for it to work. https://codepen.io/tsjohns9/pen/MvmZzB?editors=1112

2 Answers

Steven Parker
Steven Parker
243,201 Points

It appears that the handlers for the mousdown and mouseup events are not installed until the first time the button is clicked, and a "click" event is not generated until the mouse is released, so it is too late to illuminate the button.

To remedy this, you could install the handlers as part of the "on" function so they would be ready for the first mousedown event on each button.

Good looking model of the classic game! Did you create that yourself?

Thanks I fixed it so it doesn't get called with a click event