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

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,242 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
}