2

I tried with .next(), .closest() .parent() but non of them worked.

Here is my html markup:

<div id="arrownav1" class="ct_as_arrow_nav ">
    <div class="ct_amy_arrows_next">
        <i class="fa fa-angle-right next-arrow"></i>
    </div>
    <div class="ct_amy_arrows_prev">
        <i class="fa fa-angle-left prev-arrow"></i>
    </div>
</div>

I am trying to show left arrow after user does the first click on the right arrow.

I hide it with:

jQuery('.ct_amy_arrows_prev').removeClass("prev_show");

Then I show it with:

jQuery('.ct_amy_arrows_prev').addClass("prev_show");

It works but since there are multiple instances of that slider on a page click on the first slider activates all left arrows on all sliders.

So I need a way to just activate that closest element and add prev_show class to it. What am I doing wrong?

The whole nav code:

//Navigation
//==================================================
function initButtons() {
  jQuery('#arrownav' + sliderID + ' .next-arrow').click(function() {
    gonext();
  });
  jQuery('#arrownav' + sliderID + ' .prev-arrow').click(function() {
    goprev();
  });
}
var stopnextslide = 0;

function gonext() {
  var stopnextslide = 0;
  var n = jQuery('#ct_as_amy_sliderid' + sliderID + ' ct_amy_section').length;

  jQuery('#ct_as_amy_sliderid' + sliderID + ' ct_amy_section').each(function() {
    if (jQuery(this).hasClass('bespoke-active') && Number(jQuery(this).attr('rel')) + 3 == n) {
      deck.slide(0);
      stopnextslide = 1;
    }
  });
  if (stopnextslide != 1) {
    jQuery(this).closest(".ct_as_arrow_nav").find(".ct_amy_arrows_prev").addClass("prev_show");
    deck.next();
  }
};

function goprev() {
  var stopnextslide = 0;

  var n = jQuery('#ct_as_amy_sliderid' + sliderID + ' ct_amy_section').length;

  jQuery('#ct_as_amy_sliderid' + sliderID + ' ct_amy_section').each(function() {
    if (jQuery(this).hasClass('bespoke-active') && Number(jQuery(this).attr('rel')) == 3) {

      deck.slide(n - 1);
      stopnextslide = 1;
    }
    if (jQuery(this).hasClass('bespoke-active') && Number(jQuery(this).attr('rel')) == 1) {

    }
  });
  if (stopnextslide != 1) {
    deck.prev();
  }
};

Thanks

ibrahim mahrir
  • 31,174
  • 5
  • 48
  • 73
Marc Sommerson
  • 99
  • 2
  • 11

3 Answers3

2

You know the structure. Just navigate up to the parent then search for the previous button. No need to use expensive operations like closest

$('.ct_amy_arrows_next').on('click', function() {
  $(this).parent().find('.ct_amy_arrows_prev').show();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="arrownav1" class="ct_as_arrow_nav ">
    <div class="ct_amy_arrows_next">
        <i class="fa fa-angle-right next-arrow">&gt;</i>
    </div>
    <div class="ct_amy_arrows_prev" style="display: none;">
        <i class="fa fa-angle-left prev-arrow">&lt;</i>
    </div>
</div>
1

You can achieve this using closest and find methods:

$('.next-row').click(function(){
    $(this).closest('.ct_as_arrow_nav').find('.ct_amy_arrows_prev').addClass("prev_show");
});

$('.next-arrow').click(function(){  
 $(this).closest('.ct_as_arrow_nav').find('.ct_amy_arrows_prev').addClass("prev_show");
});
.prev_show{
   color:red;
}
i{
   font-size:30px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="http://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet"/>
<div id="arrownav1" class="ct_as_arrow_nav ">
    <div class="ct_amy_arrows_next">
        <i class="fa fa-angle-right next-arrow"></i>
    </div>
    <div class="ct_amy_arrows_prev">
        <i class="fa fa-angle-left prev-arrow"></i>
    </div>
</div>
<div id="arrownav2" class="ct_as_arrow_nav ">
    <div class="ct_amy_arrows_next">
        <i class="fa fa-angle-right next-arrow"></i>
    </div>
    <div class="ct_amy_arrows_prev">
        <i class="fa fa-angle-left prev-arrow"></i>
    </div>
</div>
Mihai Alexandru-Ionut
  • 47,092
  • 13
  • 101
  • 128
0

You can do it like this:

$(this).closest(".ct_as_arrow_nav")
       .find(".ct_amy_arrows_prev")
       .removeClass("prev_show");;

Or like this:

$(this).parent()
       .next(".ct_amy_arrows_prev")
       .removeClass("prev_show");

Note: If the navs are dynamically created then consider binding the events like this!

Edit:

Change your initButtons to this:

function initButtons() {
  jQuery('#arrownav' + sliderID + ' .next-arrow').click(gonext);
  jQuery('#arrownav' + sliderID + ' .prev-arrow').click(goprev);
} 

So that gonewt and goprev will have this bound to the arrow buttons.

Community
  • 1
  • 1
ibrahim mahrir
  • 31,174
  • 5
  • 48
  • 73