user2733016 user2733016 - 1 year ago 143
Javascript Question

Applying styles to elements inserted with InnerHTML in Polymer

I have some element that contains content of element in it and I want to format it and the only way that worked is changing InnerHTML. However, local stylesheet doesn't apply to changed content. How to fix that? Here's the part of the code of a custom element created with Polymer:

attached: function() {
this.querySelector(".post-body").innerHTML = this.format(this.querySelector(".post-body").innerHTML);
format: function(text) {
return text.replace(/\n(.+)\n/g, "<p>$1</p>");

In this example I get the right formatting but my stylesheet styles don't apply to the P element inside it. Is there anyway to fix it like re-applying styles to page or anything else?

Answer Source

After updating the HTML call this.updateStyles().

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download