0

I need to get the count of all elements on the page which have display property set to block. But I don't have to use JQuery I need to do it in pure JavaScript

Rifat Cholakov
  • 111
  • 1
  • 1
  • 11

2 Answers2

6

In a comment you've said:

I have a list of items(li elements) on the html and I need to get the count of all li elements which have display property block

and

the elements are styled from an external css file

This means you'll have to individually check each element with getComputedStyle (or on obsolete versions of IE, currentStyle).

You said you have a list, so if I assume you have a NodeList or HTMLCollection, we can use Array.prototype.reduce:

var count = Array.prototype.reduce.call(yourList, function(count, element) {
    return count + (getComputedStyle(element).display === "block" ? 1 : 0);
}, 0);

If you have to support obsolete versions of IE, you'll want to check for currentStyle:

var getStyle = window.getComputedStyle || function getStyle(element) {
    return element.currentStyle;
};
var count = Array.prototype.reduce.call(yourList, function(count, element) {
    return count + (getStyle(element).display === "block" ? 1 : 0);
}, 0);

If you need a list of them rather than just the count (you've said "count," but...), use filter instead:

var getStyle = window.getComputedStyle || function getStyle(element) {
    return element.currentStyle;
};
var filtered = Array.prototype.filter.call(yourList, function(element) {
    return getStyle(element).display === "block";
});
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

Please try it:

const elementsWithDisplayBlockProperty = []
document.querySelectorAll("body *").forEach(element => {
    window.getComputedStyle(element).getPropertyValue("display") === "block" && elementsWithDisplayBlockProperty.push(element)
})

console.log(elementsWithDisplayBlockProperty) // result
Ali Torki
  • 1,929
  • 16
  • 26
  • Clearly the winner, providing a modern ECMAScript 6 solution. Only criticism could be that the OP asks for a count. Logging elementsWithDisplayBlockProperty.length would provide that. – Gerard Sep 02 '18 at 11:41
  • 2
    @Gerard unlike the other answer there is 0 explanation as *please try it* is never a good answer – Temani Afif Sep 02 '18 at 11:43
  • @Gerard - There's no particular call for ES2015+ here, so I didn't use any (we're not quite at the point we can assume people can target ES2015+ environments yet); it's easy enough to switch to arrow functions if desired. The above, though, 1. Has no explanation. 2. Uses `forEach` where it should use `filter` (or `reduce`, if we take the OP at their word about what they want: a count). 3. Uses `&&` side-effects where it should use `if`. 4. Assumes the OP doesn't need to support IE8 or -shudder- IE9-11 "compatibility mode" (which, sadly, isn't a valid assumption even in 2018). – T.J. Crowder Sep 02 '18 at 11:57
  • @T.J.Crowder 1. If repeating OP's remarks and indicating which code block to copy depending on what the output should be is an explanation: Agreed. 2. forEach is a valid option if nothing needs to be returned from the function. Because of the && and array push, no return statement is necessary. 3. See 2. 4. Agreed. 5. Agreed. (I wonder how many references the OP clicked on. My guess is zero.) Anyway, sorry if I stepped on anybody's toes. – Gerard Sep 02 '18 at 12:11
  • @Gerard - No toes stepped on. :-) The above is just not remotely a "good answer." No explanation (no, code dumping is not "explanation"), poor practices... – T.J. Crowder Sep 02 '18 at 12:14