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

Output value to class

Hey guys, following script would output result to #value element, but I need it to output to class of certain element so that it loads progress bar, for example class is "load-1", "load-2", "load-3" and so on, do you have any idea of how to do that?

                        function animateValue(id, start, end, duration) {
                            var range = end - start;
                            var current = start;
                            var increment = end > start? 1 : +1;
                            var stepTime = Math.abs(Math.floor(duration / range));
                            var obj = document.getElementById(id);
                            var timer = setInterval(function() {
                                current += increment;
                                obj.innerHTML = current;
                                if (current == end) {
                                    clearInterval(timer);
                                }
                            }, stepTime);
                        }

                        animateValue("value", 0, 25, 10000);

1 Answer

andi mitre
STAFF
andi mitre
Treehouse Guest Teacher

Use jQuery's .toggleClass() based on the values you get. For each specific range, toggle the specified class.

Cheers

Or if you prefer vanilla JS, classList is your friend.