Select the five previous siblings in reverse order using jQuery's slice method

I have a unique approach to displaying a series of divs where only 5 are shown at a time using the slice() method. To navigate through the items, I include an ellipsis (...) link after every 4th item, which allows users to easily move on to the next set of 5 items. If you're having trouble picturing it, here's how it would look with all the items displayed instead of just the 5:
< 1 2 3 4 … 5 6 7 8 … 9 10 11 12 … 13 14 15 16 >

The challenge arises when a user wants to go back and view the previous 5 divs. Clicking the back arrow should hide the current set and reveal the preceding 5 divs. While slice() doesn't allow for reverse functionality, selecting the previous 5 siblings presents another hurdle. Using prev() or nth-child won't quite solve this issue as desired. As someone who considers themselves beyond a beginner in jQuery but not quite a Jedi yet, I'm stuck on finding a solution without overcomplicating the function. Check out my work-in-progress fiddle here:
https://jsfiddle.net/nehLuj98/

$(".btn-nav").slice(0,5).show();
$(".btn-ellipsis").each(function(){
    var goNum = $(this).prev(".btn-num");
    var num = Number(goNum.attr("id"));
    var currentNum = $(this).find(".current");
    $(this).click(function(){
        $(this).hide();
        $(".btn-num").hide().slice(num,(num+4)).show().next(".btn-ellipsis").show();
        $(this).next(".current").removeClass("current");
        $(".btn-num").hide().slice(num,(num+4)).show().next(".btn-ellipsis").show();
        goNum.add("#"+(num+1)+"").addClass("current");
    });
    $(".btn-prev").click(function(){
        //Here's where I'd like to add the functionality to show the previous 5 items
        //$(".btn-nav").add(".btn-ellipsis").hide();
    });
});

Since I'm working with AngularJS, the dynamic generation of numbers from a .json file might play a role in this feature. The insertion of the ellipsis link every 4th numbered item accommodates variations in the number of links available. For simplification purposes, I've included it directly in the html for the fiddle.

TL;DR
Do you know of a way to achieve reverse functionality using jQuery's slice() method or select the previous 5 siblings differently?

Answer №1

If you review your existing code, one possible solution would be:

$(".btn-prev").click(function(){
    if ($('.current').attr('id') === '1' || $('.current').attr('id') === '4')
    return;
  var currentElip = $('.btn-ellipsis:visible');
    if (!currentElip.length)
    currentElip = $('.btn-next');
  else
    currentElip.hide();
  var goNum = currentElip.prevAll('.btn-ellipsis').prev(".btn-num");
  var num = Number(goNum.attr("id"));
  $(".btn-num").hide().slice(num-4,(num)).show().next(".btn-ellipsis").show();
    $(".current").removeClass("current");
    $("#"+(num)).addClass("current");
});

You can find detailed explanations and comments regarding the above code in this fiddle:

https://jsfiddle.net/nehLuj98/10/

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

Returning a Response in HapiJS Routes from Incoming Requests

Currently, I am facing an issue with the request module where I am able to successfully make a request and receive a response. However, I am struggling to figure out how to pass this response back to my route in order to return it to my client. Here is th ...

Handling Camera Positioning and Direction in Three.js: Utilizing Orbit Controls and camera

Having trouble getting Orbit Controls to function correctly after setting the camera to : camera.up.set(0,0,1) This results in improper orbiting behavior, and there are some unresolved questions online addressing this issue: Three.js: way to change the u ...

Developing dynamic tabs with AJAX updates on the frontend in Magento

I'm looking to create ajax updated tabs for the user profile tab on the frontend of Magento, similar to the categories tab in product management. Here is the code I have used: <ul id="page_tabs" class="tabs"> <li> <a id="p ...

Client-side filtering of jqGrid with multiple conditions

I am faced with a challenge in filtering records in a jqGrid based on multiple conditions. For example, if there are columns for Name, Age, and City, and I need to filter the grid using the following condition: Name = "Mark" and Age = 25 and City = &apos ...

Button submission for modifying several files

Current Scenario: On my article page (article.php), I have divided the content into two separate sections - one for the title (pulled from title.php) and another for the content (pulled from content.php). I recently added a feature that allows the author ...

Is there a way to retrieve the size of a three.js group element?

Obtaining the dimensions of a mesh (Three.Mesh) can be done using the following code: mymesh.geometry.computeBoundingBox() var bbox = mymesh.geometry.boundingBox; var bboxWidth = bbox.max.x - bbox.min.x; var bboxHeight = bbox.max.y - bbox.min.y; var bbo ...

Utilize the npm module directly in your codebase

I am seeking guidance on how to import the source code from vue-form-generator in order to make some modifications. As a newcomer to Node and Javascript, I am feeling quite lost. Can someone assist me with the necessary steps? Since my Vue project utilize ...

The expansion feature of PrimeNG Turbotable

I'm currently facing an issue with the Primeng Turbotable where I am unable to expand all rows by default. You can find a code example of my problem at this link. I have already tried implementing the solution provided in this example, but unfortuna ...

Effective ways to manage extensive forms in Angular 2

I have a complex form in my application. What is the most effective way to collect data from this form and transmit it to the API using Angular 5? ...

Having issues with Angular Material, specifically with mat-list-item and routerLinkActive not functioning as expected

Currently, I am working with a navigation view that utilizes the MatSidenavModule. The issue I am encountering is on mobile screens. When I click a mat-list-item, the mat-sidenav closes as expected. However, upon opening the mat-sidenav again, Material alw ...

What are some examples of utilizing the hash feature in AngularJS when using $q.all?

After researching extensively, I have come up empty-handed in my search for a clear explanation on how to utilize this feature. The provided documentation states: This method returns a single promise that will return an array or hash of values, each val ...

AJAX request experiences significant delays due to MySQL query execution

The issue There is a significant delay in the execution of AJAX requests that involve any database queries. This problem has suddenly emerged without any recent updates to the codebase. Interestingly, if the same query is executed on a webpage directly (e ...

Tips for resizing a tooltip using CSS

I am currently working on customizing tooltips and would like them to display right below the hover text in a responsive manner, with the ability to have multiline text. Instead of spreading horizontally, I want the tooltip to expand vertically based on th ...

Modifying the position of an HTML element within its parent tag using document.createElement

As I dive into learning Javascript, I've come across document.createElement. This method allows me to create an HTML tag and insert it into an existing HTML parent element. However, I have a specific question in mind: Is it possible to choose the exac ...

What is the correct way to display my data in a table using XSLT?

Here is the desired look I'm aiming for I am still learning about xml/xsl, so please point out any errors kindly (: Below is a snippet from an xml file: <weather yyyymmdd="20200616"> <year>2020</year> <month>06< ...

What is the reason for not being able to locate the controller of the necessary directive within AngularJS?

A couple of angularjs directives were written, with one being nested inside the other. Here are the scripts for the directives: module.directive('foo', [ '$log', function($log) { return { restrict: 'E', r ...

Learn how to extract values from an object in Vue JS time-slots-generator and display either PM or AM based on the

Using the time-slots-generator package, I am able to display the time from 0:15 to 24:00. However, the issue is that this package does not have built-in functionality to display AM/PM, so I had to create a manual solution for it. To achieve this, I modifi ...

Is there an easy method to compare the CSS styles of a specific section in HTML?

Currently, I am facing an issue where the size of a specific area on my asp.net page changes after a post-back. It seems that the style applied to this area is being altered for some reason. This situation has raised the question in my mind - is there a m ...

Discover the step-by-step process of retrieving an image through jQuery AJAX in Laravel 8

I'm facing an issue while trying to download images using jQuery/AJAX in Laravel 8. Although I am able to retrieve the image in the response, it isn't downloading as expected. Could someone please assist me in resolving this problem? Controller ...

When jQuery inserts JavaScript into the DOM, it does not automatically execute the code

UPDATE: I initially used filter(), which was incorrect. I have since switched to find(), but unfortunately the issue persists. This snippet of code functions properly for me: $('#content') .replaceWith($( '<div> <d ...