0
Hello, I would like to take all the Divs called "card" and check a text within the div, the value of the text set as width of another div. Example
:
But I only managed to make it work in one "card", in others nothing happens.
JS Code:
function getPercentage(){
cards = document.querySelectorAll(".card");
cards.forEach(card => {
ValPercentage = document.querySelector(".percentage").innerHTML;
document.querySelector(".progress").style.width = ValPercentage;
});
};
getPercentage();
HTML code:
<div class="card">
<img src="images/html5.svg" alt="HTML logo">
<div class="card-info">
<div class="card-title">
<h2>HTML</h2>
<p>Avançado</p>
</div>
<div class="card-percentage">
<div class="progress-bar"><div class="progress"></div></div>
<h2 class="percentage">90%</h2>
</div>
</div>
The goal is to take the text of each H2.percentage and set as the width of the div.Progress, but with the code I did the result only occurs with the first div card, nothing happens with the other Divs card. What should I do?
It almost worked, however he is changing the width of the class "Progress-bar", the right would be he change the width of the class "Progress"
– AndersonPGS
you’re right :) I edited the question, the
previousElementSibling
points to the div "Progress-bar", and thefirstChild
for the div "Progress", which is the div’s first "son"– Ricardo Pontual
worked perfectly, thank you very much :)
– AndersonPGS