Develop a dynamic progress bar using jQuery

I am having trouble creating a percentage bar using jquery and css. The code I have written is not working as expected, particularly the use of jquery css({}). Here is the code snippet:

*'width' : width - seems to be causing issues for me

//vote percentage
$('.vote-percent').each(function() {
  var percentValue = $(this).find('.percent-num').text();
  $(this).find('.percent-bar').attr('data-percentage', percentValue);
  var width = $(this).find('.percent-bar').attr('data-percentage');
  $(this).find('.percent-bar').css({
    'width': width
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="vote-percent flx justify-items-r">
  <span class="percent-num">65%</span>
  <div class="percent-bar"></div>
</div>

<div class="vote-percent flx justify-items-r">
  <span class="percent-num">25%</span>
  <div class="percent-bar"></div>
</div>
<div class="vote-percent flx justify-items-r">
  <span class="percent-num">10%</span>
  <div class="percent-bar"></div>
</div>

Answer №1

I have streamlined your code by removing some unnecessary steps and added a simple styling to make the bars visible.

$('.vote-percent').each(function(){
    var percentValue = $(this).find('.percent-num').text();
    $(this).find('.percent-bar').css('width', percentValue);
});
.percent-bar {
    background-color: red;
    height: 10px
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="vote-percent flx justify-items-r">
    <span class="percent-num">65%</span>
    <div class="percent-bar"></div>
</div>
<div class="vote-percent flx justify-items-r">
    <span class="percent-num">25%</span>
    <div class="percent-bar"></div>
</div>
<div class="vote-percent flx justify-items-r">
    <span class="percent-num">10%</span>
    <div class="percent-bar"></div>
</div>

Answer №2

Instead of reading from the HTML text directly, it is recommended to utilize the data attribute for data retrieval.

// Displaying vote percentages
$('.vote-percent').each(function() {
  var width = $(this).data('percentage');
  $(this).find('.percent-num').text(width);
  $(this).find('.percent-bar').css({ width });
});
.percent-bar {
  background: #3c3f50;
  height: 30px;
  border-radius: 5px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="vote-percent flx justify-items-r" data-percentage="65%">
  <span class="percent-num"></span>
  <div class="percent-bar"></div>
</div>

<div class="vote-percent flx justify-items-r" data-percentage="25%">
  <span class="percent-num"></span>
  <div class="percent-bar"></div>
</div>
<div class="vote-percent flx justify-items-r" data-percentage="10%">
  <span class="percent-num"></span>
  <div class="percent-bar"></div>
</div>

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

The issue of the keyboard disappearing on Chrome for Android while switching between different input

Issue with Input Switching on Chrome for Android I am facing difficulty when trying to switch between two input fields. The HTML code is as follows: <input type="text"/> <input type="text"/> When I enter text in the first input and click on ...

Issues arises when trying to send an array of favorite items to a view using PHP and AJAX

Could you please assist me with sending this array of favorites to PHP using AJAX? I keep encountering a 403 forbidden error, even though the path seems correct. It's possible that I am missing something here, any guidance or help would be highly appr ...

Unique calculation for rotational movement

I am currently developing a unique compass application. Although the project is progressing well, I am facing a significant challenge with one aspect: My code calculates degree angles within the range of -360 and 360: -318°, -29°, 223°, -163°, ... ...

Looking to turn off animation for a WordPress theme element?

Is there a code that I can insert into the style.css file to deactivate the theme animations on my WordPress page? As users scroll down, each element either drops in or slides in. I recently purchased the WP Alchemy theme from , but the animation speed is ...

"Trouble with image height not scaling correctly in Internet Explorer when using the img tag

I am facing an issue that can be observed by visiting this link using Internet Explorer. Interestingly, everything functions perfectly in all other browsers (although Chrome displays little blue squares next to the images for some unknown reason). Here i ...

Issues with functionality arise when cloning HTML divs using JQuery

VIDEO I created a feature where clicking a button allows users to duplicate a note div. However, the copied note does not function like the original - it's not draggable and changing the color of the copied note affects the original note's color. ...

Switching markdown data into HTML within a Django and React application

I am considering the best way to store blog content in my database so that I can easily display it on an HTML page using an AJAX call. After conducting research, I have found suggestions to store the blog post as markdown, which seems logical since it supp ...

Steps for creating a square button

Is it possible to create a square button on my website that still functions like a traditional button? I want the button to change appearance slightly when scrolled over. Currently, I have an example where the button is not clickable: http://jsfiddle.net ...

Exception being raised despite handling in JQuery AJAX post request

I am facing an issue with my login JSON service that handles username and password. When I make a call using JQuery 1.10.1 AJAX and input the correct credentials, it functions correctly. However, if I input incorrect credentials, the error function handler ...

Having difficulty parsing or assigning JSON response: The response is being interpreted as [object Object]

Working with the Flickr API, Javascript/JQuery, and AJAX, I have the following code: function getRequest(arguments) { var requestinfo; $.ajax({ type: 'GET', url: flickrurl + '&'+ ...

Utilize Javascript to create a function that organizes numbers in ascending order

Is there a way to modify this code so that the flip clock digits appear in ascending order rather than randomly? $( '.count' ).flip( Math.floor( Math.random() * 10 ) ); setInterval(function(){ $( '.count' ).flip( Math.floor( Math.rand ...

When inserting a page break after a section, it seamlessly flows to the next page during printing

When should the CSS properties page-break-after: always or before be used? I have tried various methods, such as creating different div elements for page-break, adjusting float and clear:both, but have not had any success. There are currently four section ...

The Django form returns as not valid during an AJAX request as form.is_valide() is False

I have a form for uploading a model that includes an ImageField, and I want users to be able to submit images via AJAX. The issue is that form.is_valid() returns False. Everything works fine when not using AJAX. I've tried several solutions from simi ...

Move the focus to the previous input field by removing the key

I have created a phone number input with 10 fields that automatically skip to the next field when you fill in each number. However, I would like to be able to go back to the previous field if I make a mistake and need to delete a digit. How can I achieve ...

Using just one "nav.php" file for both the homepage and subdirectories

Looking to enhance the efficiency of my websites, I have implemented a PHP file for navigation, footer, sidebar, and other elements. However, this current method involves using two separate files: one for the homepage and another for the remaining pages wi ...

Save room for text that shows up on its own

I am currently dealing with a situation where text appears conditionally and when it does, it causes the rest of the page to be pushed down. Does anyone know the best way to reserve the space for this text even when it's not visible so that I can pre ...

Doesn't seem like jQuery's .blur() is responsive on dynamically created fields

I am currently designing a login form that includes a registration form as well. Using jQuery, the email field is generated dynamically. The labels are positioned below the fields so that they can be placed inside the field. As the field gains focus or ha ...

An innovative countdown clock for WooCommerce that dynamically displays delivery time as a customizable shortcode

After successfully creating a Wordpress shortcode with a JavaScript counter, I encountered an issue: Back End - Counter functions properly: https://i.stack.imgur.com/qyHIL.png Front End - Counter not working (no console errors...): https://i.stack.imgu ...

What is the best way to use shortcodes to display custom fields for post objects, specifically products, within WordPress posts

I'm in the process of displaying 'featured products' within my blog posts. These specific products need to be chosen using custom post objects on the backend for each individual post. I've outlined what I believe the PHP code should lo ...

Turn off jQuery on certain pages while keeping the styling intact?

Hey there, I'm currently in the process of developing a mobile website using jquery. I was wondering if it's possible to disable jQuery on certain pages while still retaining the styles, such as the navigation bar. The reason for wanting to disab ...