Add navigation dots to the slider in order to align them with the specified div element

Visit this JSFiddle link for the code

<html>
  <link href="./style.css" rel="stylesheet" type="text/css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>     
<div class="divs">
    <div class="cls1">
        <div>Yes 1 </div>
        <div>Element 1 </div>
    </div>
    <div class="cls2">
        <div>Yes 2 </div>
        <div>Element 2 </div>
    </div>
    <div class="cls3">
        <div>Yes 3 </div>
        <div>Element 3 </div>
    </div>

</div>
<a id="next">next</a>
<a id="prev">prev</a>
<br>
<br>
<br>
<br>
<li class="nav-dots">
    <label class="nav-dot" id="img-dot-1" onclick=""></label>
    <label class="nav-dot" id="img-dot-2"></label>
    <label class="nav-dot" id="img-dot-3"></label>
  </li>

<script>
$(document).ready(function(){
    $(".divs>div").each(function(e) {
        if (e != 0)
          $(this).hide();
    });
    
    $("#next").click(function(){
      const visibleDiv = $(".divs>div:visible");
      const nextDiv = visibleDiv.next();
      if(nextDiv.length > 0) {
        visibleDiv.hide();
        nextDiv.show();
      }

    });

    $("#prev").click(function(){
      const visibleDiv = $(".divs>div:visible");
      const prevDiv = visibleDiv.prev();
      if(prevDiv.length > 0) {
        visibleDiv.hide();
        prevDiv.show();
      }
    });
});

</script>
</html>

Clicking the "Next" button should correspond to the dots, and clicking the dots should change the slides accordingly. If you have a solution using OnClick, please share as I couldn't figure it out how to do it.

Answer №1

Check out a live demo of this feature on this fiddle: https://jsfiddle.net/dh8sw2fn/

In order to make the dots correspond to the current slide being displayed, I have applied a CSS class called .nav-dot--active for styling purposes. Initially, this class is assigned to the first dot and then updated within the selectSlide function.

To allow users to click on the dots to navigate to different slides, we can utilize button elements along with the aria-label attribute to enhance accessibility support.

You can set up the click event handlers like so:

$(".nav-dot").each((index) => {
    $(this).click(() => {
         selectSlide(index);
    });
});

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

Uncovering the Solution: Digging into a Nested ng-repeat to Access an Array in AngularJS

I am working with a recursive array in JavaScript: [{ type: 'cond', conditionId: 1, conditions: undefined }, { type: 'cond', conditionId: 2, conditions: undefined }, { type: 'group', conditionId: undefined, ...

Passing the value in a td element to a JavaScript function using Thymeleaf onClick

Trying to utilize "Thymeleaf" for the first time, I am attempting to pass a value to JavaScript with the following code: onclick="getPropId('${properties.id}')" The corresponding function is as follows: getPropId(inputID){alert(inputId);} Unf ...

The white-space property disrupts the normal width of elements

My initial goal was to stack two elements together side-by-side, both taking the full available height. One element has a fixed width of 200px with an image (70px wide) inside, while the other element should only fit one line of text, clipping any overflow ...

Experiencing a PHP Contact Form Problem - Unable to Identify the 400 Error Cause

Greetings! I'm fairly new to all this website creation stuff and I'm currently working on setting up a contact form from a template that I stumbled upon. Despite my best efforts in configuring the PHP code, I keep encountering a 400 response erro ...

PHP: The constant ENT_HTML5 is not defined and has been assumed as 'ENT_HTML5'

I encountered the following error message: Use of undefined constant ENT_HTML5 - assumed 'ENT_HTML5' in /blahblahpath/application/models/Post.php on line 12 This error occurred in a basic model that handles some POST input in a forum applicatio ...

The function Array.map is unavailable, causing the datalist to not update as expected

I've been closely following a tutorial series on YouTube, but I keep encountering an error during the process. The link to the tutorial is available here: https://www.youtube.com/watch?v=re3OIOr9dJI&t=11s&ab_channel=PedroTech. The purpose of ...

What is the best way to send JSON data from a function within an AMD module?

Why do I keep receiving "undefined" when trying to check for the existence of my json object? AFTER REVISION: After thoroughly reading the post referenced here: How do I return the response from an asynchronous call?, I came across a valuable insight. Tow ...

The Bootstrap div is struggling to maintain its width on smaller screens

My attempt to create 3 inputs with the following code: <div class="col-sm-5 col-md-6"> <form> <div class="form-row"> <div class="col-0.5 search-label"> <label class="control-label">Search :</lab ...

Event bubbling does not occur in elements outside of the DOM

I am facing an issue with a DOM element that has not been added to the DOM yet, but I want to trigger a DOM event on it. The event is not supposed to bubble up, however, when using jQuery, it does. This behavior seems odd to me. This strange phenomenon c ...

Is it possible to create an observable with RXJS that emits only when the number of items currently emitted by the source observables matches?

I am dealing with two observables, obs1 and obs2, that continuously emit items without completing. I anticipate that both of them will emit the same number of items over time, but I cannot predict which one will emit first. I am in need of an observable th ...

Interactive pop-up windows in Bootstrap

I encountered an issue with bootstrap modal forms where the form displays correctly after clicking on the button, but the area in which the form is displayed gets blocked! It's difficult to explain, but you can see the problem by visiting this link. ...

How should endpoint functions be correctly written for Mongoose in conjunction with Express?

While developing the backend API for my app, I have come across numerous examples of different approaches to handling errors in endpoint functions. The two options presented below illustrate this: Option 1: export const deleteProject = asyncHandler(async ...

Ways to detect and respond to events in this particular scenario

I'm creating necessary components dynamically based on the provided CSS. This process involves iterating through a response array and generating HTML elements accordingly. for (var i = 0; i < responseinner.length; i++) { for (var k = 0; k < ...

What is the best way to place a parent div above its child element?

I'm currently dealing with a container div styled with background-color: red;. This container has around 12 children, and the last child is set with background-color: blue;. My goal was to position the container on top of the child with the blue backg ...

Is it possible to utilize a route path in a JavaScript AJAX request?

So I have a situation with an ajax call that is currently functioning in a .js file, utilizing: ... update: function(){ $.ajax({ url: '/groups/order_links', ... However, my preference would be to use the route path instead. To achieve ...

A guide on converting HTML and CSS to a PDF file using JavaScript

I'm facing a challenge where I need to create a custom quote in pdf using data retrieved in JavaScript and sent in HTML. However, the issue is that no library supports CSS for the PDF generation process. After some research, I came across HTML2CANVAS ...

Task that merges multiple JSON files into one using Gulp

Within my directory, I have a collection of JSON files that contain arrays of objects. Folder A file1.json file2.json file3.json All the files in this folder have the same structure (a single array containing multiple objects). My goal is to execu ...

What is the best way to add custom text to the URL input field of the CKEditor image/link dialog?

Hey everyone, I've been working with CKEditor 4 on my project and I'm facing a challenge. I need to insert a string into the URL textfield in the image or link dialog window using JavaScript/jQuery. Here's where I'm stuck: I can't ...

Challenges with utilizing multiple backgrounds in CSS

<!DOCTYPE html> <html lang="en"> <head> <meta name="description" content=""> <meta name="author" content=""> <title>Main Website</title> <link href="css/main.css" rel="stylesheet"> </head> ...

Issue with Datatables when using less than 6 columns

Currently, I have been utilizing the Datatables plugin in my Laravel 5.2 project. The table is populated using jQuery and Ajax post methods. This represents the structure of my HTML table: <table id="entidadeTable" class="table"> <thead> ...