JavaScript Performant way to find out if an element or any of its parent element has display: none

I need to find a very performant way to find out if a custom element or any of its parent elements has display: none;

First approach:

checkVisible() {
  let parentNodes = [];
  let el = this;
  while (!!(el = el.parentNode)) {
    parentNodes.push(el);
  }
  return [this, ...parentNodes].some(el => el.style.display === 'none') 
}

Is there anything that runs faster than this? Is this even a safe method?

The reason I need this: We have a <data-table> custom element (native webcomponent) which does very heavy lifting in its connectedCallback(). We have an application that has like 20-30 of those custom elements in a single page, which leads to IE 11 taking like 15 seconds until the page is rendered.

I need to delay initialisation of those <data-table> components which are initially not even visible, so I need a way to test inside the connectedCallback() if the element is visible (which it is not if it is in one of the 18 tabs initially not shown).

Answer:1

The easiest way to see if an element or its parent has display:none is to use el.offsetParent.

const p1 = document.getElementById('parent1');
const p2 = document.getElementById('parent2');
const c1 = document.getElementById('child1');
const c2 = document.getElementById('child2');
const btn = document.getElementById('btn');
const output = document.getElementById('output');

function renderVisibility() {
  const p1state = isElementVisible(p1) ? 'is visible' : 'is not visible';
  const p2state = isElementVisible(p2) ? 'is visible' : 'is not visible';
  const c1state = isElementVisible(c1) ? 'is visible' : 'is not visible';
  const c2state = isElementVisible(c2) ? 'is visible' : 'is not visible';
  
  output.innerHTML = `Parent 1 ${p1state}<br>Parent 2 ${p2state}<br/>Child 1 ${c1state}<br/>Child 2 ${c2state}`;
}

function isElementVisible(el) {
  return !!el.offsetParent;
}

function toggle() {
  p1.style.display = (p1.style.display ? '' : 'none');
  p2.style.display = (p2.style.display ? '' : 'none');
  renderVisibility();
}

btn.addEventListener('click', toggle),
renderVisibility();
<div id="parent1" style="display:none">
  <div id="child1">child 1</div>
</div>
<div id="parent2">
  <div id="child2">second child</div>
</div>
<button id="btn">Toggle</button>
<hr>
<div id="output"></div>
Answer:2

How can I share pdf file link or image link which will open in react native mobile app web browser. I have added a share button but when the user clicks on it, it opens where to share menu options ...

How can I share pdf file link or image link which will open in react native mobile app web browser. I have added a share button but when the user clicks on it, it opens where to share menu options ...

<div id="parent"> <div id="child1"> <div id="child2"> <div id="child3"> <div id="child4"> </div> </...

<div id="parent"> <div id="child1"> <div id="child2"> <div id="child3"> <div id="child4"> </div> </...

  1. javascript parent elements
  2. javascript get parent elements
  3. javascript traverse parent elements
  4. javascript multiple parent elements
  5. javascript all parent elements
  6. javascript remove parent elements
  7. javascript iframe parent elements
  8. javascript get all parent elements
  9. javascript find all parent elements
  10. javascript find elements parent

As an example, let's say I've a component that can take in props like this: const testComponent = (props: {isBold: boolean}) => { if(props.isBold) return <strong><div>hello</...

As an example, let's say I've a component that can take in props like this: const testComponent = (props: {isBold: boolean}) => { if(props.isBold) return <strong><div>hello</...

What I'm trying to achieve here is intellisense/autocomplete for an object that's been generated from an array - something like an Action Creator for Redux, an array of strings (string[]) that can be ...

What I'm trying to achieve here is intellisense/autocomplete for an object that's been generated from an array - something like an Action Creator for Redux, an array of strings (string[]) that can be ...