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

Can't get JavaScript code to work the way I want it to

Hello all,

I'm not necessarily looking for someone to just show me how to do it, but more so show me why what I'm going is wrong. The code works, but only once. After, I click next, it will go to the next slide, but then it stops working after that - for both "next" and "previous".

HTML

<!DOCTYPE HTML>
<div id="port_examples" class="row">
  <div id="port_ex_1" class="">
    <div id="port_text_1" class="">
      <p> Lorem ipsum</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">
    <div id="port_text_2">
         <p> Lorem ipsum</p>
  </div>
  <div id="port_img_2">
    <div class="arrows" >
      <button id="right-arrow" type="button"> Prev </ button>
      <button id="left-arrow" type="button"> Next </ button>
    </div>
  </div>
</div>

CSS

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

JS

<script>
$(document).ready(function() {
    var $curr = $(".port:not('hide')");
    var $start = $('#port_ex_1'); 
    var $next = ($curr.next().length); 
    var $prev = ($curr.prev().length);
    var myArray = $(".port").length;

    function getNext () {
        if($curr.next().length) {
            $curr.fadeOut('slow').addClass('hide');
            $curr.next().fadeIn('slow').removeClass('hide');
        } else {
            $curr.fadeOut('slow').addClass('hide');
            $('.port').first().removeClass('hide');
        }
    }

    function getPrev() {
        if($curr.prev().length) {
            $curr.fadeOut('slow').addClass('hide');
            $curr.prev().fadeIn('slow').removeClass('hide');
        } else {
            $curr.fadeOut('slow').addClass('hide');
            $curr.last().removeClass('hide');
            }
    }


    $("#port_examples a img").on('click', function() {
        var $arrow = $(this).attr('id');
        alert($curr.next());

        if($arrow === "right-arrow") {
            getNext();
        } 
        if($arrow === "left-arrow"){
            getPrev();
        }

    }); 
});

</script>

Here's a link to the Jsfiddle - I couldn't get it to work on here for some reason. https://jsfiddle.net/alexflores67/domvst89/1/#&togetherjs=KGqCv78f64

Any help would be greatly appreciated!

1 Answer

Steven Parker
Steven Parker
231,269 Points

You attached click handlers to the images in the first slide, but you did not attach any handlers to the buttons in the second slide. So once you go to the second slide, there is nothing exposed that can be triggered.

But I'm a bit surprised this even works for the first slide. Your first-slide images (img) and second-slide buttons have the same id names. Every element id on a page must be unique.

Alex Flores
Alex Flores
7,864 Points

Well I'm not actually calling the IDs in the script. I could just take them out, but good catch. Thanks