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

This doesn't seem to be working

I have a click event in my JavaScript in which these statements occur:

        if (!event.target.nextSibling.nextSibling.nextSibling.value) event.target.nextSibling.nextSibling.nextSibling.innerHTML = 1; 
        else {
            event.target.nextSibling.nextSibling.nextSibling.innerHTML = parseInt(event.target.nextSibling.nextSibling.nextSibling.innerHTML) + 1;
        }

This should check if the specified element has a value. If it doesn't, it becomes 1. Otherwise it becomes its current value plus 1. However, it doesn't seem to work. When I click it once, the value becomes 1, but if I click it again, it becomes 11, then 111. This tells me that parseInt() is probably not working, but that could possibly not be the only problem. Anyone any ideas?

2 Answers

Steven Parker
Steven Parker
243,670 Points

Without seeing the HTML this is a bit of a guess; but offhand, I can't think of any kind of element that I would sample the "value" attribute of and then set the "innerHTML". That might relate to the issue.

Also, and again this depends on the HTML, but perhaps instead of "nextSibling" you might need "nextElementSibling".

For a more accurate answer, please show all the code, or better yet, make a snapshot of your workspace and post the link to it here.

Yes, because you are adding a one every time you click it. The "+" operator is kinda of tricky. To add up do or increment, it would look like this. a = a + 1; which would be 1 = 1 + 1, the short hand to this is "++"; The only reason I can see as to why it is doing this without seeing the rest of you code is because it is not an int but a string, '1' + 1 = '11'

Steven Parker
Steven Parker
243,670 Points

But "parseInt" returns a number (or a "NaN")! How could it be a string?