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

Alex Flores
Alex Flores
7,864 Points

Please help me... I have no idea why this isn't working

I've been working on this ALLL day and I can't figure this out. I think the issue is, because $curr isn't actually registering the current div that it's on.

If someone could at least point me in the right direction.. please. thank you

btw, I'm trying to make a simple next/previous slide...

You can find my non-working code at jsfiddle (https://jsfiddle.net/#&togetherjs=KGqCv78f64)

or here it is

HTML

<!DOCTYPE HTML>
<div id="port_ex_1" class="port">
                                    <div id="port_text_1">
                                        <p> Portfolio 1 </p>
                                    </div>
                                    <div id="port_img_1" > 
                                        <div class="arrows" >
                                            <a href="#"><img href="#" id="left-arrow" src="#"> prev </a>
                                            <a href="#"><img href="#" id="right-arrow" src="#"> next </a>
                                        </div>
                                    </div>
                                </div>
                                <div id="port_ex_2" class="hide port">
                                    <div id="port_text_1">
                                        <p> PORTFOLIO 2 </p>
                                    </div>
                                    <div id="port_img_2">
                                        <div class="arrows" >
                                            <a href="#"><img href="#" id="left-arrow" src="#"> prev </a>
                                            <a href="#"><img href="#" id="right-arrow" src="#"> next </a>
                                        </div>
                                    </div>
                                </div>

css

<style>

.hide {
    display: none;
}
</style>

JS

<script>
    var $curr = $(".port:not('hide')");


    $('#left-arrow').on('click', function() {
  alert("hello");
        var i = $curr.index();
            i--;

    $curr.addClass('hide');
        $('.port').eq(i).removeClass('hide');
        }); 

    $('#right-arrow').on('click', function() {
        var i = $curr.index();

             i = i >= $('.port').length-1 ? 0 : i+1;
            alert($('.port').length);

        $curr.addClass('hide');
        $('.port').eq(i).removeClass('hide');

    }); 
</script>

1 Answer

Steven Parker
Steven Parker
229,608 Points

As I already pointed out, Every element id on a page must be unique. You still have duplicated id's ("left-arrow" and "right-arrow"), which prevent some of your handlers from getting attached.

Also, your selector ".port:not('hide')" doesn't filter anything, since hide is not an element tag name. You probably want ".port:not(.hide)".