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

'To Do List' app with jquery

Hi,

I'm working on the 'To Do List' application from the 'Interactive Web pages with Javascript' course. I'm trying to rewrite it with jQuery and I've come across an issue I can't understand or solve. When more than one items are added to the incomplete tasks list, the 'edit' function only works for the even ones i.e. if there are 4 of them there, only numbers 2 and 4 are editable.

I created a codepen for this http://codepen.io/kkoutoup/pen/gbQbQX

I would appreciate any help I can get on this

1 Answer

Calling $('.editButton').click(...) adds a click event handler to every edit button on the page, including those that already have one. Your code does this every time the add button is pressed. Any previously added handlers are not removed. Thus a single click may cause multiple calls to the same handler. The item ends up in editing mode only if the number of calls is odd. You can see this clearly by logging the mode (editing or not) to the console inside the handler.

By the way, the comment on line 40 is misleading. It's the else block that ends there, not 'add button function'. And as the rest of the code is outside of that else block, the handlers are added even if the input was not valid.

jvikman Thanks very much for the feedback. I thought I wasn't getting an answer to this. I resolved this by using delegation so even the list items that are dynamically added to the lists get the event handlers (click,change etc.). Seems to be working....