Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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.