I am attempting to create an animation in JavaScript for multiple Bootstrap progress bars, each based on their individual widths. However, I have encountered an issue where the code only takes the value of the first DOM element with the class "progress-bar." Can someone please provide guidance on how to resolve this issue? Thank you in advance :)
Below is my code:
HTML
<h5>Coffee Demand</h5>
<div class="progress">
<div class="progress-bar progress-bar-danger" role="progressbar" aria-valuenow="70" aria-valuemin="0" aria-valuemax="100" style="width:70%">
70% Complete (danger)
</div>
</div>
<h5>English Language</h5>
<div class="progress">
<div class="progress-bar progress-bar-danger" role="progressbar" aria-valuenow="70" aria-valuemin="0" aria-valuemax="100" style="width:70%">
C1 Level (danger)
</div>
</div>
<h5>German Language</h5>
<div class="progress">
<div class="progress-bar progress-bar-warning" role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width:60%">
A2 Level
</div>
</div>
JS
$(function () {
var cWidth = $('.progress-bar').css("width")
$(".progress-bar").animate({width: cWidth}, 1500)
});
EDIT I was able to fix the issue by using a modified version of the code provided by @streetlamp.
However, I am still not completely satisfied as there is a slight freeze at the beginning of the animation. Any suggestions on how to make it smoother?
$(".progress-bar").css("width", "0px")
$(function() {
$(".progress-bar").each(function() {
var finalWidth = parseInt($(this).attr("aria-valuenow"));
$(this).css("width", "0px").animate({width: finalWidth+"%"}, 1000);
});
});
Thank you for all your assistance thus far.