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

Anthony Ho
Anthony Ho
10,228 Points

I'm trying to make the number of nav dots equal number of images in the slider.

HTML:

<div class="sl-slider">

 <div class="sl-slide bg-1" >
 <div class="sl-slide-inner">
                            <img src="img/1.jpg">

                        </div>
                    </div>

    <div class="sl-slide bg-1">
            <div class="sl-slide-inner">
                            <img src= "img/2.jpg">
                </div>
                    </div>

    <div class="sl-slide bg-1" >
            <div class="sl-slide-inner">
                            <img src="img/3.jpg">

                        </div>
                    </div>

    <div class="sl-slide bg-1" >
            <div class="sl-slide-inner">
                            <img src= "img/4.jpg">
                        </div>
                    </div>


                </div>

<nav id="nav-dots" class="nav-dots">
                    <span class="nav-dot-current"></span>
                    <span></span>
                    <span></span>
                    <span></span>

                </nav>

JS:

<script>

            var count = $(".sl-slider > div").length; 
            while(count){
                $(".nav-dots").append('<span>');
            }
            return false

        </script>

What I'm trying to do is count the number of child elements in the sl-slider and append the number of <span> elements under nav.

1 Answer

Ryan Field
PLUS
Ryan Field
Courses Plus Student 21,240 Points

Hi, Anthony. In a while clause, it will be evaluated and repeated until the statement inside the parentheses is false. In your case, you have a single number, which means it will evaluate as true infinitely. For this case, I'd personally use a for loop, like this:

for (var i = 0; i < count; i++) {
   $(".nav-dots").append('<span></span>');  //<-- closed your span tag
}