bskool bskool - 1 month ago 16
Javascript Question

How to add/remove class using bullets and arrows

I am having trouble figuring out how to add and remove a class from a div when a user clicks either on its corresponding number or the prev/next arrows.

Notice the 'active' class on the second item as well as the second nav. I would like the class to be removed and added to the first item and nav when a user clicks the "1" or the "previous" arrow.



.wrap {
display: block;
width: 200px;
margin: 50px auto;
text-align: center;
}

.nav {
margin-bottom: 10px;
}
.nav a {
padding: 5px;
}
.nav a:hover {
font-weight: 900;
cursor: pointer;
}
.nav a.active {
border-bottom: 1px solid black;
font-weight: 900;
}

.prev, .next {
display: inline-block;
padding: 5px;
}
.prev:hover, .next:hover {
font-weight: 900;
cursor: pointer;
}

.items div {
display: none;
padding: 25px;
}
.items .one {
background: red;
}
.items .two {
background: green;
}
.items .three {
background: blue;
}
.items .active {
display: inline-block;
}

<div class="wrap">
<div class="nav">
<a class="one">1</a>
<a class="two active">2</a>
<a class="three">3 </a>
</div>
<div class="items">
<span class="prev"><</span>
<div class="one">ONE</div>
<div class="two active">TWO</div>
<div class="three">THREE</div>
<span class="next">></span>
</div>
</div>





http://codepen.io/bbbenji/pen/WovJEM

Answer

You can try somethinglike this:

$('.next, .prev').on('click', function() {
  var $cur = $(this).closest('.wrap').find('.items div.active');
  var $nav = $(this).closest('.wrap').find(' .nav a.active')

  if ($(this).hasClass('next')) {
    if ($cur.next('div').length === 0) return
    $cur.next('div').addClass('active');
    $nav.next('a').addClass('active');
  } else {
    if ($cur.prev('div').length === 0) return
    $cur.prev('div').addClass('active');
    $nav.prev('a').addClass('active');
  }
  $cur.removeClass('active')
  $nav.removeClass('active')
})

$('.nav a').on('click', function() {
  var index = $(this).index();
  $(this).closest('.wrap').find('.items div').removeClass('active').eq(index).addClass('active')
  $(this).closest('.wrap').find('.nav a').removeClass('active').eq(index).addClass('active')
})
.wrap {
  display: block;
  width: 200px;
  margin: 50px auto;
  text-align: center;
}
.nav {
  margin-bottom: 10px;
}
.nav a {
  padding: 5px;
}
.nav a:hover {
  font-weight: 900;
  cursor: pointer;
}
.nav a.active {
  border-bottom: 1px solid black;
  font-weight: 900;
}
.prev,
.next {
  display: inline-block;
  padding: 5px;
}
.prev:hover,
.next:hover {
  font-weight: 900;
  cursor: pointer;
}
.items div {
  display: none;
  padding: 25px;
}
.items .one {
  background: red;
}
.items .two {
  background: green;
}
.items .three {
  background: blue;
}
.items .active {
  display: inline-block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<div class="wrap">
  <div class="nav">
    <a class="one">1</a>
    <a class="two active">2</a>
    <a class="three">3 </a>
  </div>
  <div class="items">
    <span class="prev"><</span>
    <div class="one">ONE</div>
    <div class="two active">TWO</div>
    <div class="three">THREE</div>
    <span class="next">></span>
  </div>
</div>

<div class="wrap">
  <div class="nav">
    <a class="one">1</a>
    <a class="two active">2</a>
    <a class="three">3 </a>
  </div>
  <div class="items">
    <span class="prev"><</span>
    <div class="one">ONE</div>
    <div class="two active">TWO</div>
    <div class="three">THREE</div>
    <span class="next">></span>
  </div>
</div>