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
Asked
Active
Viewed 883 times
0
-
check https://stackoverflow.com/a/3260313/7580839 – Amir Fo Sep 02 '18 at 11:00
-
2Possible duplicate of [Select only (display:block) element from list of items Jquery](https://stackoverflow.com/questions/36931716/select-only-displayblock-element-from-list-of-items-jquery) – לבני מלכה Sep 02 '18 at 11:00
-
Set *directly* (via the `style` attribute and/or property) or by a stylesheet? What research have you done? What attempt at a solution have you made? – T.J. Crowder Sep 02 '18 at 11:00
-
@לבנימלכה - The OP said *without* jQuery. – T.J. Crowder Sep 02 '18 at 11:01
-
1@T.J.Crowder look at marked answer and see it some with javascript – לבני מלכה Sep 02 '18 at 11:03
-
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 – Rifat Cholakov Sep 02 '18 at 11:04
-
@לבנימלכה - "with JavaScript" != "without jQuery". But yes, I see an answer without jQuery that will work only for inline styles. – T.J. Crowder Sep 02 '18 at 11:05
-
the elements are styled from an external css file. They are not styled inline – Rifat Cholakov Sep 02 '18 at 11:06
-
Possible duplicate of [Get only visible element using pure javascript](https://stackoverflow.com/questions/44612141/get-only-visible-element-using-pure-javascript) – seebiscuit Sep 02 '18 at 11:09
2 Answers
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 allli
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