Skip to content Skip to sidebar Skip to footer

How To Call A Function In Polymer After The Elements Have Been Created (like Ready Function In Jquery)

I'm trying to write a function, which is related to the DOM, but the problem is , polymer calls the function before the DOM is loaded, and as expected it gives me an undefined erro

Solution 1:

You could do something like this:

var domReady = function (func) {
    if ('complete' === document.readyState) {
        func();

        return;
    }

    document.addEventListener('DOMContentLoaded', function () {
        func && func();
    });
};

domReady(function () {
    // Your code
});

Solution 2:

In Polymer 2.0 you can use this into connectedCalback life cycle method:

connectedCallback(){
   super.connectedCallback();  
   Polymer.RenderStatus.beforeNextRender(this, function() {
       // All references to the dom elements into the web component// [...]
   });
}

Post a Comment for "How To Call A Function In Polymer After The Elements Have Been Created (like Ready Function In Jquery)"