Files
30-seconds-of-code/snippets/getElementsBiggerThanViewport.md
Itamar Yatom 84d2584218 Update getElementsBiggerThanViewport.md
There is no reason to use reduce when when you don't need to.
`.filter()` is much simpler to reason, easier to read and more verbose
2020-10-08 14:36:30 +03:00

21 lines
649 B
Markdown

---
title: getElementsBiggerThanViewport
tags: browser,intermediate
---
Returns an array of HTML elements whose width is larger than that of the viewport's.
- Use `HTMLElement.prototype.offsetWidth()` to get the width of the `document`.
- Use `Array.prototype.filter()` on the result of `document.querySelectorAll()` to check the width of all elements in the document.
```js
const getElementsBiggerThanViewport = () => {
const docWidth = document.documentElement.offsetWidth;
return [...document.querySelectorAll('*')].filter(el => el.offsetWidth > docWidth);
}
```
```js
getElementsBiggerThanViewport(); // <div id="ultra-wide-item" />
```