Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

custom elements & connectedCallback() : wait for parent node to be available before firing a function

I'm using custom elements, which are very nice. But I'm facing a problem :

When the connectedCallback() function is called, it seems that the node is not yet at its place in the DOM, thus I cannot access its parents - and I need them.

class myElement extends HTMLElement{
    constructor() {
        super();
        this.tracklist =    undefined;
    }
    connectedCallback(){
        this.render();
    }
    render(){

        this.tracklist = this.closest('section');

        // following code requires this.tracklist!
        // ...
    }

window.customElements.define('my-element', myElement);

How could I be sure the parent nodes are accessible before calling render() ?

Thanks !

like image 787
gordie Avatar asked Oct 17 '25 05:10

gordie


1 Answers

Long answer moved to a single StackOverflow answer where all updates and new knowlegde is kept: https://stackoverflow.com/a/70952159/2520800

like image 127
Danny '365CSI' Engelman Avatar answered Oct 19 '25 20:10

Danny '365CSI' Engelman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!