What could be causing the progress bar to malfunction?

I have encountered an issue with my basic progress bar. It seems to work fine when I include only one progress element in my HTML page. However, if I add another one, the first one works as expected while the second one does not.

You can view my code on Codepen

HTML:

<div class="ani-progressbar">
    <div class="fill-progress" kac-puan="8">
      <span></span>
      <span></span>
      <span></span>
      <span></span>
      <span></span>
      <span></span>
      <span></span>
      <span></span>
      <span></span>
      <span></span>
  </div>
</div>
<div class="ani-progressbar">
    <div class="fill-progress" kac-puan="10">
      <span></span>
      <span></span>
      <span></span>
      <span></span>
      <span></span>
      <span></span>
      <span></span>
      <span></span>
      <span></span>
      <span></span>
  </div>
</div>
<script src="https://code.jquery.com/jquery-3.1.0.min.js"></script>

CSS:

.ani-progressbar{
  width:100%;
  height:5px;
  margin-bottom:10px;
}
.fill-progress span{
  display:block;
  width:5px;
  height:5px;
  border:1px solid #9ad204;
  float:left;
  margin-left:1px;
}

jQuery:

$(document).ready(function(){

  var getVal = $(".ani-progressbar .fill-progress").attr("kac-puan");
  $(".ani-progressbar .fill-progress span").slice(0,getVal).each(function(index,element){

   $(this).css("background","#9ad204");
  });

});

Answer №1

If you want your jQuery function to work correctly, make sure to update it like so:

$(document).ready(function() {
    $(".ani-progressbar .fill-progress").each(function() {
        var getVal = $(this).attr("kac-puan");
        var thisParent = $(this);
        thisParent.children('span').slice(0, getVal).each(function(index, element) {
            $(this).css("background", "#9ad204");
        });
    });

});
  • Make sure to retrieve the attr of each progress-bar rather than just the first one, as this is why your function may not be working for all instances.

  • Additionally, store the current progress-bar in a variable named thisParent and use the slice method to target all children span elements based on their respective kac-puan attr values.

To see the updated code, check out this link to my revised Codepen

Answer №2

The reason is that you are selecting all span elements and then slicing them, which means only the first one will work correctly.

To see a working example, visit this link: https://jsfiddle.net/hrv27bx3/

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

How to properly align a form with a multi-lined label in HTML and CSS?

I'm looking to create a form label that spans multiple lines and align the inputs with the labels. The goal is for the "Releasse Date" label to display as: Release Date (YYYY-MM-DD): [input box] Here's how it currently looks: https://i.sstatic ...

Issue with Angular: Undefined Variable

After starting to work with Angular Routes recently, I encountered a situation where data was getting lost on refresh. The solution suggested to me was to use resolve, which seemed promising but I am struggling to implement it successfully. .config([&a ...

Getting the text from a Selenium element can become tricky when dealing with product discounts that alter the element

(Programming) I am working on selecting random products from a website, some with discounts and some without. For instance: How do I retrieve product 748 (without discount)? or How do I retrieve product 419 (with discount)? When the product ha ...

The React-Big-Calendar Drag and Drop feature in the month view consistently drags events from the leftmost column

I'm seeking assistance with a bug I've encountered while using the big-react-calendar. The issue arises when dragging an event, as it consistently moves to the leftmost column regardless of mouse position. However, shifting the event to a differe ...

Troubleshooting issue with refreshing selectpicker in Bootstrap-select and Vue.js

Incorporating the bootstrap-select plugin (found at ) into a ruby on rails app with Vue.js as the javascript framework has been my latest project. The goal is to have two select options where one selects a category and the other displays all available tea ...

How to dynamically insert li elements into a ul list using the .after() method

After trying to add an li element to the ul list, I noticed that my DOM wasn't updating when checking the page source. Even attempting to delete elements with the delete button didn't seem to work as expected. Here is the code snippet below. ...

Retrieve the value from another select element

Is there a way to create a function in pure JavaScript that changes the selected options in two select tags based on each other? For example, if I choose a French word in one select tag, the English equivalent automatically changes in the other select tag ...

Inquiry about CSS Media Queries

I am facing an issue with CSS media queries... For my HTML page, I have separate files for desktop and iPad styles - desktop.css and ipad.css. In desktop.css, I have used @import url("ipad.css"); and added a @media not only screen and (device-width:768px) ...

Utilizing an NPM package in your project

Trying to incorporate a node module called "tagify" into my node.js application has been challenging. Following the instructions in the package's readme file (https://www.npmjs.com/package/@yaireo/tagify#installation), I executed the setup steps as ou ...

What could be causing my CSS navbar with display: inline-block to not center properly?

How can I use display:inline-block in CSS to center my navigation bar? nav { position: fixed; width: 100%; } nav ul { width: 100%; margin: auto; } nav ul li { display: inline-block; margin: 5px 20px 5px 20px; text-align: center; } ...

Troubleshooting a config file problem related to prefixes

While exploring discord.js at , I encountered a problem that has me stuck. Index.js const Discord = require('discord.js'); const { prefix, token } = require('./config.json'); const client = new Discord.Client(); client.on('ready& ...

terminate server through the router

I have created an express application like the following: const express = require('express') const app = express(); app.use(express.static(path.join(__dirname, 'public'))); app.post('/close', async (_, res) => { res.sta ...

Having trouble with passing props in React. Any suggestions on what I might be missing?

It seems like I am facing an issue with passing props in React, and for some reason, it's not functioning as expected. I'm a bit puzzled by the whole situation. Main Application Component import React from 'react' import Produc ...

Storing the order of jQuery UI Sortable in the WordPress Admin Panel

My goal is to customize the order of taxonomy terms for each individual post in Wordpress. I have created a metabox on the EDIT POST page that contains custom taxonomy terms and made them sortable using JQuery. Here is my setup on the Wordpress backend: ...

Submitting a form in React/Javascript: A step-by-step guide

I have a website on Wordpress where I am using the Wp-Polls plugin to vote. How can I submit a post request by accessing a form URL in a React project? Specifically, how can I vote for the "Bad" option with a value of 2? The HTML structure of the WordPre ...

Firebase Authentication error code "auth/invalid-email" occurs when the email address provided is not in a valid format, triggering the message "The email address is

Currently, I am working on integrating Firebase login and registration functionality into my Angular and Ionic 4 application. Registration of user accounts and password retrieval are functioning properly as I can see the accounts in my Firebase console. Ho ...

What is the best way to prevent multiple Material UI cards from expanding simultaneously?

I currently have a dilemma with my Material UI expandable cards. Although everything is functioning correctly, both cards expand simultaneously, which is not the desired behavior. This is how my configuration file is structured: module.exports = { featu ...

Div content with a unique angle

I need to create a website with slanted div elements that must meet the following criteria: The sections should have a height of approximately 400px when displayed side by side, and about 800px when they stack vertically. It would be ideal if this could ...

Tips for avoiding repeated Modal Popup instances and preventing the page from automatically scrolling to the last element when using ReactJS

I've been working on a project where I'm fetching data from a server and displaying 10 different sets of data in Bootstrap cards using the map() function. Each card contains a button to open a modal, along with a Link that shows the route related ...

Can Firebase lists be reversed?

I have searched extensively on SO for an answer to this question, but I haven't found a solution yet. Therefore, please do not mark this as a duplicate. Currently, I am working with AngularFire in Angular 2 and Typescript. I am using FirebaseListObse ...