What is the process for altering the active status in pagination?

How can I make the active class in my pagination appear in red? Here's the code snippet I have:

In my script.js file:

$(".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();
}

And in my style.css file:

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

The issue I'm facing is that the <a> tag only turns red when clicked, but reverts back to blue once the mouse is released. How can I make sure it stays red while active?

Answer №1

Avoid utilizing the pseudo-class :active and opt for selecting the element by its class using .active.

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

Answer №2

To see the result, you can test it using the CSS code snippet provided below: See It in Action here

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

Answer №3

Give it a try and see if this can be of assistance.

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>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error thrown: SyntaxError - Forbidden break statement in AJAX code execution

Trying to exit a loop nested within a statement has been a challenge. Despite researching similar questions on stackoverflow, I have not found a solution that works. Below is the current code in question: for (var i = 0; (i < 10); i++) { ...

Node.js - Passport authentication consistently results in redirection to failure route

I am currently working on creating a login form using passportJs, but I keep encountering the issue of failureRedirect. Despite searching on stack overflow for a solution, I have not found the correct answer yet. Below is my code: Here is how I am crea ...

What is the best way to assign two styles with the same value in a single line of code?

Can you suggest a simpler method for expressing left:15%;right:15%, such as using left,right:15% or something equivalent? Your assistance is greatly appreciated! ...

Undefined is returned after resolving - React - JavaScript API

Learning React and JavaScript is a bit challenging for me, especially when it comes to understanding how Promises and resolving work. I'm currently working on an API to log into an application with an internal SQL database. The queries are functionin ...

Executing a jQuery AJAX request within a script that has been imported from a different domain

Yes, I understand the concept of the same domain policy and how JQuery can work around it using JSONP. However, I am facing a slightly different issue. Browsers typically restrict cross-domain content calls due to the "same domain policy." That makes se ...

Encountering an Unexpected Token Error While Parsing JSON with jQuery

Utilizing the Envato API with jQuery to collect user stats is my current project. An example JSON response that I will display is as follows: { "new-files-from-user":[ { "thumbnail":"http://3.s3.envato.com/files/60560.jpg", "tags":"", "use ...

Issue retrieving Select2 tag values from view to controller in ASP.NET MVC 5

I need assistance in transferring tag values from the view to the controller. Here is my approach: I have a model attribute for fetching Select2 values: In the model: public string[] TagList { get; set; } In the view, I have a select element like this: ...

Problems with Searching in Bootstrap Tables

I'm experiencing a basic bootstrap error. I attempted to create a searchable table using this example: Unfortunately, the search function is not working when applied to my table. The table appears fully populated, but entering search terms like "CRY" ...

Insert some text into the div element. Create a new line

I'm looking to make a specific text on my webpage trigger a new line when displayed in a div. I've been struggling to figure out how to accomplish this: var original= "the text @n to change"; var changed = original.replace(/@n /g, '\ ...

I wish to adjust the font size as well as resize the table elements simultaneously

Adjusting the height and width of the table should automatically adjust the font size as well. <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>jQuery UI Resizable - Default functiona ...

How can I style an image in CSS to have a set height, maximum width, and keep its aspect ratio fixed?

I am trying to place a set of images with varying heights and widths inside a div tag. My goal is to have the images maintain a fixed height, unless the width surpasses a certain limit, in which case I want the height to adjust while preserving the aspect ...

Using jQuery's toggle function with a double click event to change the display to none

A div I created has the ability to expand to full screen upon double click, and I now wish to implement a toggle function so it can return to its original size when double clicked again. Initially, the code successfully increased the size of the div. Howe ...

What is the best way to trigger a re-render of a Vue component after a change in Vuex

I'm interested in developing a custom component to modify the basemap of a leaflet map without relying on L.control.layers. To achieve this, I created a sidebar component that offers options to switch between different basemaps. In my implementation, ...

After installation, the Tailwind classes seem to be malfunctioning

Let me start by mentioning that although the title of this question is reminiscent of this other question on Stack Overflow, I have already tried the solutions provided there with no success. I initially attempted to set up Tailwind using their official C ...

search through an object to find specific data

Here is a JavaScript object that I have: var data = { "type": [ "car", "bike" ], "wheels": [ "4", "2" ], "open": [ "Jan", "Jan" ] ...

What is the best way to crop an image to focus solely on the center portion?

Using Bootstrap 3, how can I display an image cropped in a .col-sm-6 grid column? The image is 720px wide. Typically, with the .img-responsive class, the image will resize proportionally when the display size changes. However, I want to crop the left and ...

When using angular's ngHide directive on a button, it will still occupy space in the

At the bottom of this HTML file, there are 3 buttons displayed. The first image illustrates the layout of the 3 buttons. The second image demonstrates what happens when the middle button in the footer is commented out. The third image shows the situat ...

I have noticed that the baseline of a Span element has shifted after updating my Chrome browser to a version that begins with

Once I updated to chrome Version 108.0.5359.94 (Official Build) (64-bit) from 107.0.5304.87 (Official Build) (64-bit), the behavior of the span element changed drastically. It shifted its baseline when multiple spans were stacked on top of each other. Exp ...

Update a JQuery function for an input field following an innerHTML command

Using JQuery to check the file size and name from an html file input field can be a useful tool. The input field in question looks like this: <div id="uploadFile_div"> <input name="source" id="imageFile" type="file" style ="border: 1px solid ...