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
Vincent Leleux
Front End Web Development Techdegree Student 8,819 PointsWhat's wrong with my jquery?
What's wrong with my jquery? ( http://codepen.io/Vince2301/pen/bZrrqk )
I don't understand why the following code is not working. if I change the value of i to 4, the if-else works. Why is it not working when the value of i is increased by clicking on the button? Thanks
var i = 1;
if(i <= 3){
$('#target1').click(function() {
$('#output1').html(i);
i++;
})}
else { $("#target1").addClass("nomore");
}
2 Answers
Craig Watson
27,930 PointsHi Vincent,
Am I right in thinking you have fixed this the pen seems to be working as you would expect :S
Craig
Vincent Leleux
Front End Web Development Techdegree Student 8,819 PointsYes indeed, but now I am repeating myself so much. I have an idea how to DRY this code, but I am not clear enough.
Any tips?
Craig Watson
27,930 PointsHi Vincent,
I actually built a somewhat similar programme code wise for a game of rock paper scissors.
http://codepen.io/Craig-Watson/pen/grqxvK?editors=0010
From first glance what you need to do to DRY things up is pull your repeating code into a separate function and then call that function when the button is clicked.
You would need to pass a the counter variable and a max number in your case.
Let me know if you get stuck I will have a bash at it on codepen :)
Craig