I would like to build a progress bar with 3 colors red, yellow, and green. Each color is linked to "not done" for red, "done with difficulties" for yellow, and "successfully done" for green.
I already built my radio button and my progress bar.
I would like that at the begin every red radio button are checked, so the progress bar would be 100% red and 0% both for yellow and green. Then when you check the yellow or green button, the progress bar update and win a percentage in one of those two colors based on the number of exercises.
I know that the problem comes from my function on click, which doesn't fit very well the radio button. As new to coding, my brain is fully melting, and I don't know how to go forward.
Here is my code for the progress bar with the radio button:
function checkRed() {
const checkBoxes = document.querySelectorAll(".red:checked");
const progress = document.querySelector(".progress-inner-red");
const checklistProgressInterval = 100 / checkBoxes.length;
let width = 0;
for (let i = 0; i < checkBoxes.length; i++) {
if (checkBoxes[i].checked) {
width += checklistProgressInterval;
}
}
progress.style.width = `${width}%`;
}
function checkYellow() {
const checkBoxes = document.querySelectorAll(".yellow:checked");
const progress = document.querySelector(".progress-inner-yellow");
const checklistProgressInterval = 100 / checkBoxes.length;
let width = 0;
for (let i = 0; i < checkBoxes.length; i++) {
if (checkBoxes[i].checked) {
width += checklistProgressInterval;
}
}
progress.style.width = `${width}%`;
if (checkBoxes == 0) {
progress.style.width = `${0}%`;
}
}
function checkGreen() {
const checkBoxes = document.querySelectorAll(".turq:checked");
const progress = document.querySelector(".progress-inner-green");
const checklistProgressInterval = 100 / checkBoxes.length;
let width = 0;
for (let i = 0; i < checkBoxes.length; i++) {
if (checkBoxes[i].checked) {
width += checklistProgressInterval;
}
}
progress.style.width = `${width}%`;
}
body {
margin: 20px 50px;
}
h1 {
font-size: 1.5em;
}
p {
margin: 0;
}
input[type="radio"] {
margin-top: 7px;
vertical-align: middle;
}
label {
display: inline-block;
font: Arial;
}
/* Main Colors
===============================*/
.turq {
background: #16A085;
}
.turq:checked {
background: #1ABC9C;
}
.turq:hover {
background: #1ABC9C;
}
.yellow {
background: #eeca5a;
}
.yellow:checked {
background: #ffdc70;
}
.yellow:hover {
background: #ffdc70;
}
.red {
background: #C0392B;
}
.red:checked {
background: #E74C3C;
}
.red:hover {
background: #E74C3C;
}
.radio {
position: relative;
-webkit-appearance: none;
-moz-appearance: none;
width: 30px;
height: 30px;
margin: 5px;
margin-top: 0px -webkit-border-radius:100px;
-moz-border-radius: 100px;
border-radius: 100px;
transition: all .3s ease-in-out;
-moz-transition: all .3s ease-in-out;
-webkit-transition: all .3s ease-in-out;
}
.radio:checked {
transition: all .3s ease-in-out;
-moz-transition: all .3s ease-in-out;
-webkit-transition: all .3s ease-in-out;
}
.radio:checked:after {
transition: all .3s ease-in-out;
-moz-transition: all .3s ease-in-out;
-webkit-transition: all .3s ease-in-out;
background: transparent;
}
.radio:after {
position: absolute;
top: 5px;
left: 5px;
width: 20px;
height: 20px;
background: #fff;
content: "";
-webkit-border-radius: 100px;
-moz-border-radius: 100px;
border-radius: 100px;
}
.progress {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
height: 1rem;
overflow: hidden;
font-size: .75rem;
background-color: #e9ecef;
border-radius: .25rem;
}
}
.progress-bar {
display: -webkit-box;
display: -ms-flexbox;
display: flex;
-webkit-box-orient: vertical;
-webkit-box-direction: normal;
-ms-flex-direction: column;
flex-direction: column;
-webkit-box-pack: center;
-ms-flex-pack: center;
justify-content: center;
color: #fff;
text-align: center;
background-color: #E74C3C;
transition: width .6s ease;
}
.progress-inner-red {
background-color: #E74C3C;
width: 0%;
height: 100%;
}
.progress-inner-yellow {
background-color: #ffdc70;
width: 0%;
height: 100%;
}
.progress-inner-green {
background-color: #16A085;
width: 0%;
height: 100%;
}
<div class="progress">
<div class="progress-bar progress-inner-red" role="progressbar" style="width: 100%" aria-valuemin="0" aria-valuemax="100"></div>
<div class="progress-bar progress-inner-yellow" role="progressbar" style="width: 0%" aria-valuemin="0" aria-valuemax="100"></div>
<div class="progress-bar progress-inner-green" role="progressbar" style="width: 0%" aria-valuemin="0" aria-valuemax="100"></div>
</div>
<div>
<input class="radio red" type="radio" name="radio1" id="radio1.1" onclick="checkRed()" checked>
<input class="radio yellow" type="radio" name="radio1" id="radio2" onclick="checkYellow()">
<input class="radio turq" type="radio" name="radio1" id="radio3" onclick="checkGreen()">
<label for "radio3"> Exercise 1<label>
</div>
<div>
<input class="radio red" type="radio" name="radio2" id="radio4" onclick="checkRed()" checked>
<input class="radio yellow" type= "radio" name="radio2" id="radio5" onclick="checkYellow()">
<input class="radio turq" type= "radio" name="radio2" id="radio6" onclick="checkGreen()">
<label for "radio6" display:"inline-block"> Exercise 2 <label>
</div>