javascript - How to not switch to the first or last "li element" when they are next in line? -
i have pagination bar , switch next button if click "next" or "prev" arrow buttons.
i wrote code stay on current "page" number if next item in list ".next" or ".prev", not working.
what missing?
$(document).ready(function() { var pageitem = $(".pagination li").not(".prev,.next"); var prev = $(".pagination li.prev"); var next = $(".pagination li.next"); pageitem.click(function() { $('li.active').removeclass("active"); $(this).addclass("active"); }); // stay on current button if next or prev button ".next" or ".prev" next.click(function() { if($('li.active').next() != next) { $('li.active').removeclass('active').next().addclass('active'); } }); prev.click(function() { if($('li.active').prev() != prev) { $('li.active').removeclass('active').prev().addclass('active'); } }); });
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <nav> <ul class="pagination"> <li class="prev"> <a href="#"><span>«</span></a> </li> <li class="active"><a href="#">1</a></li> <li><a href="#">2</a></li> <li><a href="#">3</a></li> <li><a href="#">4</a></li> <li><a href="#">5</a></li> <li class="next"> <a href="#"><span>»</span></a> </li> </ul> </nav>
all need check class name, please check below updated code
$(document).ready(function() { var pageitem = $(".pagination li").not(".prev,.next"); var prev = $(".pagination li.prev"); var next = $(".pagination li.next"); pageitem.click(function() { $('li.active').removeclass("active"); $(this).addclass("active"); }); // stay on current button if next or prev button ".next" or ".prev" next.click(function() { if($('li.active').next().attr('class') != 'next') { $('li.active').removeclass('active').next().addclass('active'); } }); prev.click(function() { if($('li.active').prev().attr('class') != 'prev') { $('li.active').removeclass('active').prev().addclass('active'); } }); });
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <nav> <ul class="pagination"> <li class="prev"> <a href="#"><span>«</span></a> </li> <li class="active"><a href="#">1</a></li> <li><a href="#">2</a></li> <li><a href="#">3</a></li> <li><a href="#">4</a></li> <li><a href="#">5</a></li> <li class="next"> <a href="#"><span>»</span></a> </li> </ul> </nav>
Comments
Post a Comment