Is it possible to use jQuery to dynamically reset values?

I am currently working on paginating a section.

The pagination is functioning properly, but I would like to know if it is possible to reset the pagination dynamically based on the number received from an input using jQuery.

For example:

In my current pagination setup, it displays numbers like 1 2 3.

Now, let's say I receive the number 100.

At this point, the pagination section should reset to 100, with 100 being the active element and the sequential numbers appearing as 100 101 102.

If 101 happens to be the last page, then it should highlight 100 as the active number and 101 as the last one.

Similarly, if I enter 50, the pagination section should reset to show numbers like 50, 51, 52 with 50 being highlighted.

I have been attempting to implement this functionality in my code snippet below.

Firstly, how can the pagination detect that a number has been entered into the input text field (as per the alert message written in JavaScript)?

Any advice or suggestions are greatly appreciated.

Answer №1

Feel free to give this a try, it might be helpful for you

$(document).ready(function(){
 $( "#paging" ).keyup(function() {
   var value = this.value;
   if(!value || value < 1){return;}
   $( ".pagination li a" ).removeClass( "active" );
   $( ".pagination li" ).each(function(index, event) {
    if(index != 0 && index != 4){
    if(index == 1){
     $( this ).children('a').addClass( "active" );
    }
    $( this ).children('a').text(value);
    value++;
   }
  });
 });
});

In the HTML code, an input box has been included

<input type='number' id='paging'></input>
 <div class="center">
  <ul class="pagination">
   <li><a href="#" id="prev">«</a></li>
   <li class="three_links"><a id="test1" href="#">1</a></li>
   <li class="three_links"><a id="test2" class="active" href="#">2</a></li>
   <li class="three_links"><a id="test3" href="#">3</a></li>
   <li><a href="#" id="next">»</a></li>
  </ul>
 </div>

The script will capture input values on keyup, and you have the flexibility to customize this event as needed. Thank you.

Answer №2

For tackling this issue, I opted for a straightforward and uncomplicated approach. I established a function that triggers upon clicking the textbox where the page number is inputted, and within that function, I executed the following code:

function updatePageNumbers()
{
   var pageNumber = 27; // Assuming 27 is the starting number
   $('#pageNumber1').html(27);
   $('#pageNumber2').html(28); // Add 1
   $('#pageNumber3').html(29); // Add 2
}

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

npm installation for phonegap failed due to shasum check discrepancy

I've attempted numerous times, but I keep encountering this error (shasum check failed) 4784 error Error: shasum check failed for C:\Users\FENGXI~1\AppData\Local\Temp\npm-7004-QbpFFte5\1387269030233-0.28223602287471 ...

Activate the horizontal scrollbar within each flex item when it is stretched beyond its original width

My goal is to have both the child-1 (blue) and child-2 (black) retain their original width when the sidebar appears by enabling horizontal scrolling upon clicking anywhere in the demo below. document.body.onclick = () => document.querySelector(&apos ...

Dynamic line breaks within an unordered list

I have a requirement where I need to display the last two li elements from an ul list on a new line if the screen width is less than 625px. The code snippet below achieves this functionality: ... ... ... ... However, this code fails valida ...

The right floating behavior with <li> elements seems to be malfunctioning

I am currently dealing with an issue in my code where it works perfectly in Firefox and IE 8, but in IE 7 the second list item is displaying with some extra space at the top instead of being on the same line. <LI style="PADDING-LEFT: 20px"> ...

Mistakes in validating email form

I'm having some difficulties creating a basic contact form. I'm following the guidelines outlined in and the initial step involves using JavaScript to highlight the input box with a red border when invalid input is entered. Unfortunately, the ...

Trigger the function upon successful completion of Stripe Checkout

I have a requirement in my Nodejs/Express API to execute a series of confirmation code once the checkout process is successfully completed in Stripe by the client. Below is the checkout function responsible for handling the checkout phase: // Checkout con ...

Automatically loading a div using jQuery after a delay of 5 seconds

I am working on a feature for my homepage that involves 4 div bars. Right now, I have them set to load upon click, but I would like to adjust it so that the first div loads when the page initially loads, and then each subsequent div loads after a certain t ...

Implementing multiple lists in a dropdown menu in React and dynamically displaying one based on the state

I am struggling to load multiple lists in options based on the selected country. I have tried various approaches but can't seem to get it right. Initially, I attempted to load all the lists and place them in <option> # lists <datalist id=&q ...

How to efficiently parse JSON objects in JavaScript after receiving them from Django

After retrieving JSON data from my Django code, the format looks like this: This is how my Django code looks: objs = AccessInfo.objects.filter(~Q(weblink='')) return HttpResponse(serializers.serialize('json', objs), mimetype="applica ...

What are the steps for implementing if-else statements in JavaScript when working with multiple dropdown lists?

I have encountered an issue while trying to display options in multiple drop-down lists. Only two words (CHENNAI AND IOS-XE, BANGALORE AND IOS-XR) are being displayed and the rest of the words are not appearing. Can someone provide assistance on fixing thi ...

Error in D3: The property "age" is not defined

Currently, I am in the process of developing a force-directed graph using D3 for visualization. One of the challenges I encountered was implementing an age filter utilizing radio buttons on my HTML page and incorporating data from an imported JSON file. T ...

pino-tea's logging feature suddenly halts after processing a few hundred lines

I've encountered an issue while streaming pino logging into pino-tee, where it stops working after around 200 to 400 lines out of the expected 1000. The code snippet below in index.js (ESM) illustrates this problem: import childProcess from "chil ...

Customizing Ng-Bootstrap styles and restructuring the HTML layout for Typeahead implementation

I have been utilizing the ng-bootstrap library from https://ng-bootstrap.github.io. I am trying to customize the dropdown styles and modify some CSS properties for the typeahead component. However, I am unable to locate where I can make these changes. Cod ...

Resolved: Angular JS resolves circular dependencies

I encountered a Circular dependency issue while attempting to make a http.post call in a http interceptor: Circular dependency found: $http <- Ace <- authInterceptor <- $http <- $templateRequest <- $compile I understand the problem, but I ...

Div elements shifted to the right instead of appearing on a new line

Running into a formatting issue while working on my personal website. New elements are not starting on a new line but rather to the right of a div containing a picture gallery. A visual representation can be seen in this screenshot... https://i.sstatic.ne ...

Activating/Deactivating Textbox on Internet Explorer 9 using Javascript

It can't be due to browser discrepancies because I also tested it in Chrome. Is there possibly an error in my code? The checkbox is supposed to disable the textbox when clicked, but that's not happening. Here is the code I am using: I placed the ...

React's performance severely lags when traversing through JSON data

I have been working on fetching a JSON API using the fetch() method and displaying it in a Reactjs app. It worked perfectly fine with smaller JSON lists containing up to 100 items. However, when I attempted to do the same with a larger list containing 900 ...

Unable to load nested iframe

When working with an HTML document, I tried to add an iframe in the body without any cross-origin restrictions (same URL). However, when I tried to do the same thing within the appended iframe, although the nested iframe element was successfully added to ...

Clicking on the Ng button will display a datepicker in Onsen UI

Is there a method to display a DatePicker by clicking on a specific div element? <input type="date" ...> This will reveal a native datepicker. ...

Connect your Flask/Dash application to a customized Bootstrap stylesheet stored locally

I have created a Flask/Dash application and I am currently linking it to an external bootstrap stylesheet as shown below: dash_app = dash.Dash(server=server, routes_pathname_prefix='/dashapp/', ...