Javascript - Dynamically HTML elements

Asked

Viewed 96 times

2

Is creating HTML elements dynamically by JAVASCRIPT a bad practice? For example:

var img = document.createElement('img')
img.setAttribute('id', 'foto')

1 answer

4


Creating elements dynamically is very common. Not bad practice, on the contrary.

What can be problematic is if these elements are not removed properly and remain in memory causing performance problems.

If you add properties to these objects you should then do delete div.algumaPropriedade, or at least div.algumaPropriedade = null;. Attributes like you are using are no problem, it is more in the case of heavy functions, objects or arrays that are associated with parts of this element and that are not sometimes removed when you use parent.removeChild(div);.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.