0

I use this code to add values that has class name. Jsfiddle. I specifically want to add values of divs with class name box and has css attribute display block.

var test = document.getElementsByClassName("box");
var sum = 0;
for (i = 0; i < test.length; i++) {
  sum += parseInt(test[i].textContent);
}
console.log(sum);
Aaab
  • 3
  • 4
  • 1
    Your code works, What is the problem? – Ram Mar 30 '22 at 20:47
  • I specifically want to add values of divs with class name box and has css attribute display block. – Aaab Mar 30 '22 at 20:49
  • 1
    You already have the class name "box. " Perhaps check for "box" and "block" classes with `document.querySelectorAll("div.box.block")`? `
    ` tags don't have values, but I believe `textContent` as you have, should work.. You can check each div's `style.display` while you're looping, but I'm not sure what it would be since the style is set by the class.
    – mykaf Mar 30 '22 at 20:51
  • This already has an answer here: https://stackoverflow.com/a/8426901/943435 – Yogi Mar 30 '22 at 20:52
  • 1
    You can also use the `getComputedStyle` method: https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle – Ram Mar 30 '22 at 21:00

0 Answers0