question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

HTMLElement offsetParent is undefined

See original GitHub issue

I have some code depending upon an HTMLElement’s offsetParent, specifically within an event listener. Code I have here:

window.addEventListener('mousemove', function(e) {
  const control = e.target.offsetParent;
  if (!control) {
    return;
  }

  const factor = this.state.duration / control.offsetWidth;
  const time = e.pageX * factor;
  actions.setCurrentTime(time);
});

However, when I test this with jest (and therefore JSDOM), I can’t seem to get offsetParent to be defined in any way.

Is this an unsupported attribute of an HTMLElement?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

35reactions
panganibanpjcommented, Feb 4, 2018

I know this is old but if someone ever find this and has trouble, I got around it doing something similar to above:

Object.defineProperty(HTMLElement.prototype, 'offsetParent', {
    get() { return this.parentNode; },
});
5reactions
NtFreXcommented, Jul 17, 2019

@panganibanpj Thanks for the code snipped. I enhanced it with some of the mozilla specs.

Object.defineProperty(HTMLElement.prototype, 'offsetParent', {
    get() {
        let element = this;
        while (!isNullOrUndefined(element) &&
                    (isNullOrUndefined(element.style) ||
                    isNullOrUndefined(element.style.display) ||
                    element.style.display.toLowerCase() !== 'none')) {
            element = element.parentNode;
        }

        if (!isNullOrUndefined(element)) {
            return null;
        }

        if (!isNullOrUndefined(this.style) && !isNullOrUndefined(this.style.position) && this.style.position.toLowerCase() === 'fixed') {
            return null;
        }

        if (this.tagName.toLowerCase() === 'html' || this.tagName.toLowerCase() === 'body') {
            return null;
        }

        return this.parentNode;
    },
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

HTMLElement.offsetParent - Web APIs | MDN
The HTMLElement.offsetParent read-only property returns a reference to the element which is the closest (nearest in the containment hierarchy) ...
Read more >
What would make offsetParent null?
offsetParent will return null if your element object hasn't been appended to the DOM yet.
Read more >
HTML DOM Element offsetParent Property
The offsetParent property returns null if the element is not visible (display="none"). Tutorial: CSS Box Model. The offsetParent. All block-level elements ...
Read more >
Google Groups
offsetParent /offsetTop/offsetLeft/offsetWidth/offsetHeight. 1058 views ... In Gecko and Edge, these attributes are only supported on HTMLElement.
Read more >
Lightning SecureElement should return null instead of undefined as ...
HTMLElement.offsetParent returns null if no other answer can be found, however Locker Services SecureElement.offsetParent returns undefined.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found