Navigating using JavaScript dot notation is a powerful way to

Is there a way to implement JavaScript or jQuery code that will assign an active class to a dot when it is clicked, while also removing the active class from any other dots in the sequence?

HTML:

<div class="dotnav">
    <div class="dot active" title="Sign Up"></div>
    <div class="dot" title="hello"></div>
    <div class="dot" title="hello"></div>
    <div class="dot" title="hello"></div>
    <div class="dot" title="hello"></div>
    <div class="dot" title="helloup"></div>
</div>

CSS:

.dotnav {
    left: 40px;
    position: absolute;
    top: 50%;
    z-index: 9999;
    display: block;
}

.dot {
    width: 16px;
    height: 16px;
    border-radius: 100%;
    border: 2px solid #CCCCCC;
    display: block;
    margin-top: 10px;
    cursor: pointer;
}

.dot:hover {
    border: 2px solid #999999;
    background-color: #C9931E;
}

.active {
    background-color: #C9931E;
}

Looking for a solution using JavaScript or jQuery where clicking on a dot assigns it the active class and removes the previous dots' active classes.

Answer №1

$('.circle').click(function() {
    $('.circle.active').toggleClass('active');
    $(this).toggleClass('active');
});

Answer №2

Here is a simple solution for achieving that:

$('.dot').on('click', function(){
    $(this).addClass('active');
});

Demo Link

Answer №3

When you click on a dot, this code will remove the active class from any currently active dots and then add the active class to the clicked dot. Here is an example:

$('.dot').click(function () {
    $('div.active').removeClass('active');
    $(this).toggleClass('active');
});

Check out this demonstration on jsFiddle.

Answer №4

// To enhance readability in complex code, use on() instead of click(); and namespace your events.
// Always remember to namespace your events for better organization.
// It's a good practice to pass the event to your anonymous function to save time if patterns are the same.
$('.dot').on('click.dot', function(e){
  // Quickly remove 'active' class from all sibling elements with a single line of code
  // This way, other 'dot' structures remain unaffected
  // If you want multiple elements to sync their behavior, consider using data-attributes to synchronize them (a new challenge for you)
  $(this).siblings('.dot').removeClass('active');
  // Now, add the 'active' class to the currently clicked element
  $(this).addClass('active');
})

Explore your original HTML and CSS on jFiddle: http://jsfiddle.net/q5p6fj5y/1/

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

Implementing Nodejs middleware across multiple files

Hello, I am currently facing an issue with my code. I have two files - server.js and index.js. In server.js, I am trying to call index.js from within app.get function, but it seems like it's not entering the app.get function block. Can someone please ...

Replace the text in a different Div

Hello everyone, I'm struggling with an issue in my coding. Here is the HTML: <a href="aboutme.php"> <div class='aboutus'> <div id="mbubble"> stuff inside here </div> </div> ...

Issue encountered: Component returning nothing error in a Next.js/React application

I'm currently working on creating image slider component using Nextjs/React and Emotion. I thought I had everything set up correctly but unfortunately, I keep encountering this common error... Error: ImageSliderContainer(...): Nothing was returned f ...

Transitioning between modals using Tabler/Bootstrap components in a ReactJS environment

Currently, I am constructing a Tabler dashboard and incorporating some ReactJS components into it. Initially, I used traditional HTML pages along with Jinja2 templates. However, I have now started integrating ReactJS for certain components. I prefer not t ...

Ways to extract text from a temporary element

Here is my HTML code: <div class="p-login-info" ng-show="loggedOut()">My text.</div> This is the corresponding JavaScript code: var e = element(by.className('p-login-info')); e.getText() .then(function(text){ var logoutText = ...

Utilizing the Page Method to Handle Warnings in $.ajax({}) Calls

I created a custom method in the .cs file of my Default.aspx page: [WebMethod] public static string ConvertToJSON(object data) { JavaScriptSerializer jsSerializer = new JavaScriptSerializer(); string json = jsSerializer.Serialize(data); retu ...

How can I trigger $stateChangeStart in Angular ui-router when using state.go({notify:false})?

My Angular SPA uses ui-router for navigation and infinite scroll functionality. I currently implement infinite scroll by utilizing the following code snippet: $state.go('someStateWithPagination', {page: page+1}, {notify: false, reload: false}) ...

Is Height Responsiveness a feature?

I'm currently designing a custom responsive WordPress theme, but I've encountered a minor issue. On my page, I have multiple boxes arranged side by side. Each box is responsive in both height and width and contains an image with overlaid text. I ...

Activating the submit button only following confirmation that the image dimensions have been verified

I've written some code that checks whether selected pictures meet specific size and dimension constraints before enabling the submit button. However, there's an issue where the button might be enabled before verifying the last image due to delays ...

Sorting in Postgres is about organizing data in a specific order

I've been working on organizing my Postgres questions database by ID. Currently, the order seems random, but I want it to be sorted by ID in ascending order. Since I'm new to Postgres, I'm not entirely sure about what I need to do... All ...

Adjust the size of the div based on a percentage of the header div

I'm looking to add a new div within the header of my WordPress site. I want this div to be a percentage of the parent div's size, specifically occupying only the right side of the header. Right now, the header contains my logo on the left side, a ...

"Enhancing User Experience with Animated Progress Bars in Three.js

Is there a way to create a progress bar animation in three.js? I've been grappling with this issue for quite some time now. I attempted to replicate the html5 video player progress bar method, but unfortunately, it doesn't seem to be compatible w ...

Addressing Memory Leakage Issues in a Basic Node.js Application

Out of sheer curiosity and as an experiment with NodeJS, I decided to write a basic program to test the Collatz Conjecture for an incredibly high number of integers. While this concept should work fine in theory, my simple code is unexpectedly facing a mem ...

Is it possible to load the content before loading the corresponding images?

With a list of 10 contacts to display, I want the contacts to load first so users can view the information before the corresponding images. The HTML structure is as follows: <table id="contactsTable" data-role="listview" data-dividertheme="c"> < ...

Dynamic classroom experience featuring a bootstrap sidebar

I am having an issue with changing the active class of the sidebar after a click event. I have tried using Bootstrap and implemented some JavaScript code, but it doesn't seem to be working properly. $(function(){ $('.sidebar1 a').filt ...

I am looking to implement datepicker options to define the minimum and maximum dates within a specific method

Hi everyone, I need help setting the date picker options to have a max and min date based on method arguments. Here is my code: <input type="text" placeholder="Salary Date" uib-datepicker-popup="M/d/yyyy" ng-model="form.SalaryDate" is-open="form.opened ...

Is it possible to create a dynamic jQuery dialog that appears after appending data without the need to reload the

I'm currently dealing with a situation where a page houses a massive table within a CRUD interface. Each link within a span activates a jQuery UI Dialog Form that pulls its content from another page. After completing an action, such as creating a new ...

At what point does a box create an inline formatting context?

According to my research, I came across a situation where a block formatting context is generated as explained in MDN:Block formatting context. I am curious about the scenarios that lead to the establishment of an inline formatting context within a box. ...

Bringing in a created class establishes the universal prototype

When working with the given react component, I noticed something interesting. Even after importing it into multiple components and calling the increment method, it seems to manipulate the same instance rather than creating separate instances. This behavior ...

Use C# to set the Message.Body to be an HTML file

I am looking to insert an entire HTML file into the email body. Currently, I am using the following code to accomplish this: message.IsBodyHtml = true; While this works for assigning content line by line like so: "Welcome to our system", "<b>Tha ...