0

I have a pagination and I want the active class to be in color red. I have this code:

script.js:

$(".pagination").append("<li class='page-item' id='previous-page'><a class='page-link' href='javascript:void(0)' aria-label=Previous><span aria-hidden=true>&raquo;</span></a></li>");
    $(".pagination").append("<li class='current-page active'><a class='page-link' href='javascript:void(0)'>" + 1 + "</a></li>");
    for (var i = 2; i <= totalPages; i++) {
      $(".pagination").append("<li class='current-page'><a class='page-link' href='javascript:void(0)'>" + i + "</a></li>"); // Insert page number into pagination tabs
    }
    $(".pagination").append("<li class='page-item' id='next-page'><a class='page-link' href='javascript:void(0)' aria-label=Next><span aria-hidden=true>&raquo;</span></a></li>");
    
    $(".pagination li.current-page").on("click", function() {
      //checks if the user is at the current page
      if($(this).hasClass("active")){
        return false;
      }else{
        //removes active class and adds it from what the user is clicking
        var currentPage = $(this).index();
        $(".pagination li").removeClass("active");
        $(this).addClass("active");
        $("#page .content").hide();
}

style.css:

.pagination a:active{
  background-color: red;
  color:red;
}

I have this code but when I clicked on a <a> tag it ony colors it red when clicked but when you release the mouse it turns blue again.

InSync
  • 4,851
  • 4
  • 8
  • 30
Dhenhice
  • 103
  • 5

3 Answers3

0

Don't use :active, but select the element by class with .active.

.pagination li.active > a {
  background-color: red;
  color: red;
}
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
0

Try with following css. Working Example https://codepen.io/sethuramancbe/pen/NWOgBNE

.pagination .active a{
background-color: red;
color: red;
}
sethuraman
  • 187
  • 7
0

Try it out. Maybe This gonna help.

const listEl = document.querySelector(".page-index");
const prevBnt = document.querySelector(".prev");
const nextBnt = document.querySelector(".next");

let currentActive = 1;

nextBnt.addEventListener("click", e => {
  currentActive++;

  if (currentActive > Array.from(listEl.children).length) {
    currentActive = Array.from(listEl.children).length;
  }
  updateActive();
});

prevBnt.addEventListener("click", e => {
  currentActive--;

  if (currentActive < 1) {
    currentActive = 1;
  }
  updateActive();
});

function updateActive() {
  Array.from(listEl.children)
    .forEach((item, idx) => {
      if (currentActive > idx) {
        removeActive();
        item.classList.add("active");
      }

    });
}

function removeActive() {
  Array.from(listEl.children)
    .forEach(item => {
      item.classList.remove("active");
    });
}
.wraper {
  max-width: 300px;
  margin: 0 auto;
  padding: 100px 20px;
}

ul {}

nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

nav ul {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  list-style-type: none;
  text-align: center;
  margin: 0px;
}

nav ul li {
  background-color: #ddd;
  border: 1px solid #999;
  padding: 3px 5px;
  cursor: pointer;
}

nav ul li.active {
  background-color: red;
}

nav button {
  cursor: pointer;
}
<!doctype html>
<html>

<head></head>

<body>
  <div class="wraper">
    <nav>
      <button type="button" class="prev">&larr;</button>
      <ul class="page-index">
        <li class="active">0</li>
        <li>1</li>
        <li>3</li>
        <li>4</li>
        <li>5</li>
      </ul>
      <button type="button" class="next">&rarr;</button>
    </nav>
  </div>
</body>

</html>
Shiplu
  • 460
  • 6
  • 13
  • Here you will find full working pagination example https://stackoverflow.com/questions/72298189/implement-pagination-in-javascript-with-limit-and-offset/76798169#76798169 – Shiplu Jul 30 '23 at 14:43