Jamil
Jamil

Reputation: 671

HTML code injection using Javascript

I have a simple html snippet that I add to the document during runtime. I am using jQuery's selectors but that should irrelevant to the issue I am facing.

When using: $("#elementID").html()

The code is displayed but with no CSS applied. The behaviour make sense since the CSS has already been loaded and applied.

My questions are: - Is there away to "refresh" a DOM element in order to re-apply CSS on it after its html content has been changed? - or is there a way to apply CSS on the code I have in the JavaScript before adding it to the DOM element?

I am aware of various JS\html templates library but at this stage I am trying to minimize the dependency on extra libraries, specially since that the snippets I am generating\injecting are very simple and I dont mind them staying in the JS code

Jamil

Upvotes: 0

Views: 399

Answers (3)

Jamil
Jamil

Reputation: 671

.trigger('create') on the element will force a re-rendering of the element if needed

Upvotes: 0

EPLKleijntjens
EPLKleijntjens

Reputation: 936

Are you sure css is not applied? Because it should be. If you add a new element to the DOM, it should just take on the styling from any css files you have loaded. How are you applying the css?

Upvotes: 0

freakish
freakish

Reputation: 56517

You are wrong. CSS is applied all the time (during runtime) - look at this jsFiddle example. You messed up with something else.

Upvotes: 1

Related Questions