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
Adam Sackfield
Courses Plus Student 19,663 PointsFor Loops
Hi i am trying to put something together for the new contest and struggling with the for loop. Below is the code followed by description.
$(document).ready(function(){
$('#form').submit(function(event){
event.preventDefault();
$('#wrapper progress').remove();
var userName = $('input[name=username]').val();
var html = 80;
$.getJSON("http://teamtreehouse.com/" + userName + ".json", function(data) {
total = data.points.total;
var pointsContainer = [];
$.each( data.points, function( key, val ) {
pointsContainer.push( "<li id='" + key + "'>" + val + "</li>" );
$('#wrapper').append('<progress data-points="' + val + '" max="' + total + '" value="' + 0 + '"></progress>')
});
$('#list').html(pointsContainer);
});
var total = $('#total').val();
function animateProg() {
$('#wrapper progress').each(function(){
$(this).data('value');
for(i=0; i < val; i++)
});
}
});
});
So basically the first part is pulling json data and adding it to progress elements data attributes. Now what i cant seem to get my head around is the animateProg() function, what I am trying to do is loop over each progress element and increment the data-value from 0 to the value of 'val' (or the amount of points earned).
So to clarify want to get the first progress element say its HTML and its value is 200. I would like a for loop to iterate over the value="0" and increase it to 200. Then on to the next one.
Not sure if it makes sense but ask me a question if you need clarity.
Thanks in Advance
1 Answer
Adam Sackfield
Courses Plus Student 19,663 PointsUPDATE - I have it looping the value up to the correct amount but how can i animate it. I can see in the inspect element that it is incrementing correctly but the progress bars are not changed until it is all complete.
Any ideas how to have it reflect in browser to show the bar progressing.
<form id="form">
<label for="username">Please Enter Your Treehouse UserName<input type="text" name="username"> </label>
<input type="submit" value="Submit">
<input type="button" id="create" value="Create">
</form>
<div id="wrapper">
</div>
<ul id="list">
</ul>
<script src="jquery.js"></script>
<script>
$(document).ready(function(){
$('#form').submit(function(event){
event.preventDefault();
$('#wrapper progress').remove();
var userName = $('input[name=username]').val();
var html = 80;
$.getJSON("http://teamtreehouse.com/" + userName + ".json", function(data) {
total = data.points.total;
var pointsContainer = [];
$.each( data.points, function( key, val ) {
pointsContainer.push( "<li id='" + key + "'>" + val + "</li>" );
$('#wrapper').append('<progress data-points="' + val + '" max="' + total + '" value="' + 0 + '"></progress>')
});
$('#list').html(pointsContainer);
});
var total = $('#total').val();
});
$('#create').click(function(event){
event.preventDefault();
$('#wrapper progress').each(function(){
var value = $(this).data('points');
var i = 0;
while(i <= value) {
$(this).attr('value', i).animate(1000);
i++;
}
});
});
});