Here is my bootstrap pagination code. Maybe it can be simplified.
const pagination = {
pre: 0,
curr: 1,
max: 6,
list: null,
cn: '',
swap() {
pagination.list.children[pagination.pre].classList.remove(pagination.cn);
pagination.list.children[pagination.curr].classList.add(pagination.cn);
},
changeBy(n) {
pagination.pre = pagination.curr;
pagination.curr += n;
if (pagination.curr < 1) {
pagination.pre = 0;
pagination.curr = 1;
} else if (pagination.curr > pagination.max) {
pagination.pre = pagination.max - 1;
pagination.curr = pagination.max;
}
pagination.swap();
},
prev() {
pagination.changeBy(-1);
},
next() {
pagination.changeBy(1);
},
activate(i) {
return () => {
pagination.pre = pagination.curr;
pagination.curr = i;
pagination.swap();
}
},
setHandler() {
pagination.list.children[0].addEventListener("click", pagination.prev);
pagination.list.children[pagination.max + 1].addEventListener("click", pagination.next);
for (let i = 1; i < (pagination.max + 1); i += 1) {
pagination.list.children[i].onclick = pagination.activate(i);
}
},
init(list, cn) {
pagination.list = list;
pagination.cn = cn;
pagination.setHandler();
}
}
let list = document.querySelector(".pagination"),
cn = "active",
init = pagination.init(list, cn);
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
<nav aria-label="Page navigation">
<ul class="pagination">
<li>
<a href="#" aria-label="Previous">
<span aria-hidden="true">«</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><a href="#">6</a></li>
<li>
<a href="#" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
</ul>
</nav>