jQuery Tab Navigation – First and Last Arrow Disabled

I’ve got a simple tabbed section with a next / previous navigation. This is all working fine. What i’m trying to do is on the first tab(tab 1) disable the previous button and on the last tab(tab 4) disable the next button. I know this can be done by adding / removing ‘disabled’ class and styling. I’m just struggling to incorporate it.

I think I need to write the code for previous button if first tab addClass ‘disabled’ else removeClass ‘disabled’ and for the next button if last tab addClass ‘disabled’ else removeClass ‘disabled’.

Here’s what I have so far.

$(document).ready(function() {

  $('ul.tabs li').click(function() {
    var tab_id = $(this).attr('data-tab');

    $('ul.tabs li').removeClass('current');
    $('.tab-content').removeClass('current');

    $(this).addClass('current');
    $("#" + tab_id).addClass('current');
  })

  $('#previous').click(function() {
    $("ul.arrownavigation li:first").addClass("disabled");//This line needs editing
    if($('ul.tabs li.current').prev().length != 0 ){
    $('ul.tabs li.current').removeClass('current').prev().addClass('current').click();
        }
  })

  $('#next').click(function() {
  $("ul.arrownavigation li:first").removeClass("disabled");//This line needs editing
    if($('ul.tabs li.current').next().length != 0 ){
    $('ul.tabs li.current').removeClass('current').next().addClass('current').click();
        }
  });

});
body {
  margin-top: 100px;
  font-family: 'Trebuchet MS', serif;
  line-height: 1.6
}

.container {
  width: 800px;
  margin: 0 auto;
}

ul.tabs {
  margin: 0px;
  padding: 0px;
  list-style: none;
}

ul.tabs li {
  background: none;
  color: #222;
  display: inline-block;
  padding: 10px 15px;
  cursor: pointer;
}

ul.tabs li.current {
  background: #ededed;
  color: #222;
}

ul.arrownavigation {
  list-style: none;
    margin: 30px 0 0 0;
  padding: 0;
}

ul.arrownavigation li {
  display: inline-block;
  background: #111;
  color: #fff;
  padding: 10px;
  cursor: pointer;
  margin-right: 10px;
}

ul.arrownavigation li.disabled {
  opacity: 0.2;
  pointer-events: none;
}

.tab-content {
  display: none;
  background: #ededed;
  padding: 15px;
}

.tab-content.current {
  display: inherit;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

<div class="container">

  <ul class="tabs">
    <li class="tab-link current" data-tab="tab-1">Tab One</li>
    <li class="tab-link" data-tab="tab-2">Tab Two</li>
    <li class="tab-link" data-tab="tab-3">Tab Three</li>
    <li class="tab-link" data-tab="tab-4">Tab Four</li>
  </ul>

  <div id="tab-1" class="tab-content current">
    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
  </div>
  <div id="tab-2" class="tab-content">
    Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
  </div>
  <div id="tab-3" class="tab-content">
    Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
  </div>
  <div id="tab-4" class="tab-content">
    Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
  </div>
  
  
  <ul class="arrownavigation">
    <li id="previous" class="disabled">Previous</li>
    <li id="next">Next</li>
  </ul>

</div>
<!-- container -->

Answer

a solution testing maxvalue and min value:

$(document).ready(function() {
  //init of times
  var idnum = 1;
  var maxtabs = $("ul.tabs li").length;
  actButtons(idnum);

  $('ul.tabs li').click(function() {
    var tab_id = $(this).attr('data-tab');
    idnum = parseInt(tab_id.replace("tab-", ""));
    actButtons(idnum);

    $('ul.tabs li').removeClass('current');
    $('.tab-content').removeClass('current');

    $(this).addClass('current');
    $("#" + tab_id).addClass('current');
  })

  $('#previous').click(function() {
    if($("#previous").hasClass("disabled"))return;
    actButtons(--idnum);
    if ($('ul.tabs li.current').prev().length != 0) {
      $('ul.tabs li.current').removeClass('current').prev().addClass('current').click();
    }
  })

  $('#next').click(function() {
    if($("#next").hasClass("disabled"))return;
    actButtons(++idnum);

    if ($('ul.tabs li.current').next().length != 0) {
      $('ul.tabs li.current').removeClass('current').next().addClass('current').click();
    }
  });

  function actButtons(idnum) {
    if (idnum == 1) {
      $("#previous").addClass("disabled");
      $("#next").removeClass("disabled");
    } else if (idnum == maxtabs) {
      $("#previous").removeClass("disabled");
      $("#next").addClass("disabled");
    } else {
      $("#previous").removeClass("disabled");
      $("#next").removeClass("disabled");
    }
  }
});
body {
  margin-top: 100px;
  font-family: 'Trebuchet MS', serif;
  line-height: 1.6
}

.container {
  width: 800px;
  margin: 0 auto;
}

ul.tabs {
  margin: 0px;
  padding: 0px;
  list-style: none;
}

ul.tabs li {
  background: none;
  color: #222;
  display: inline-block;
  padding: 10px 15px;
  cursor: pointer;
}

ul.tabs li.current {
  background: #ededed;
  color: #222;
}

ul.arrownavigation {
  list-style: none;
  margin: 30px 0 0 0;
  padding: 0;
}

ul.arrownavigation li {
  display: inline-block;
  background: #111;
  color: #fff;
  padding: 10px;
  cursor: pointer;
  margin-right: 10px;
}

ul.arrownavigation li.disabled {
  opacity: 0.2;
  pointer-events: none;
}

.tab-content {
  display: none;
  background: #ededed;
  padding: 15px;
}

.tab-content.current {
  display: inherit;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>

<div class="container">

  <ul class="tabs">
    <li class="tab-link current" data-tab="tab-1">Tab One</li>
    <li class="tab-link" data-tab="tab-2">Tab Two</li>
    <li class="tab-link" data-tab="tab-3">Tab Three</li>
    <li class="tab-link" data-tab="tab-4">Tab Four</li>
  </ul>

  <div id="tab-1" class="tab-content current">
    Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
  </div>
  <div id="tab-2" class="tab-content">
    Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
  </div>
  <div id="tab-3" class="tab-content">
    Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
  </div>
  <div id="tab-4" class="tab-content">
    Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
  </div>


  <ul class="arrownavigation">
    <li id="previous" class="disabled">Previous</li>
    <li id="next">Next</li>
  </ul>

</div>
<!-- container -->

Leave a Reply

Your email address will not be published. Required fields are marked *