dynamic data in the DOM with css and js effects

ref – https://stackoverflow.com/questions/34896106/attach-event-to-dynamic-elements-in-javascript

index.html

1) we write an HTML page with a ul list that has three items.
2) apply some js effects to our li elements
3) create a button that would dynamically add a li item

script.jsjs

We have a simple script that attaches inline css and js effects to all the li elements.

Now, when you run the page, you’ll see the list with its effects. Specifically, some js added inline CSS effects and a click handler that displays the text of the li element.

This is because when the page has finished putting its DOM together, it runs the script.js file, and it also declares the addItem function.

The execution of script.js file makes it so that it applies the inline CSS and click handler to the existing li elements in the DOM.

The declaration of the addItem function exists to that later when the user clicks on the add button, we have a function that can run.

Thus, all js code executed so far comes after the readiness of the DOM.

Now, when you click on the li element, it would respond to the click event like so:

However, there is a problem. When you click on the ‘add’ button to dynamically add an item, you don’t see the inline css effects anymore. If you try clicking on the li elements, nothing happens:

CSS

Inline CSS is when we attach the CSS to the element itself.
Internal CSS is when we declare CSS on top of the html page.
External CSS is when we attach the CSS code in a separate file.

Thus, when we use inline CSS, it only applies to the loaded DOM. Any dynamic new data that comes in after won’t get the CSS effects.

In order to have CSS effects on all elements, including later added dynamic elements, simply use internal or external CSS.

In your script.js remove the css:

Then in the head tag of the html page, add some internal CSS:

Now, when you add dynamic li elements, you’ll see tht the CSS is applied immediately:

Note that you can also apply some inline CSS when the items were created and added to the DOM. That will work too, but it’s cumbersome, and we miss out on the benefits that CSS brings us:

Events were not applied

If you were to click on the li items, you’ll notice that even though internal/external CSS will aways apply to past, current, and future DOM changes, JS events won’t be applied to future DOM additions.

In order to apply it, we’ll implement an add event listener. It simply listens for a certain event, and when that event happens, it executes code.

script.js

Now when you refresh the page, every newly added li elements will the click event.

In jQuery, you’d use the .on function

In this way, all dynamically added elements will have your chosen events bound.

In certain situations when you are working with templates, a lot of effects are applied by JS on data elements. When these data elements are added dynamically, you won’t see your effects for these newly added elements. What you have to do is:

1) Make sure effects on data that are added/removed are CSS based.
2) Using addEventListener or .on, you’ll have to apply these effects on the fly.