Add HTML elements dynamically
So document.createElement is used with an HTML tag to create the element. The textContent is then modified and then the class attribute is modified using setAttribute . This could also be used to add a data attribute or any other kind of attribute, like you can in HTML. Finally the element is appended to the body using the body element's appendChild method.
Actually, It's essentially equivalent to < H1 class="note" > New Heading!!! < /h1 > .
The power of what we seen today is that we are creating and styling elements "on the fly". It's your decision to either add all the elements on the page while designing or you can create and insert HTML elements at runtime, that is dynamically using the createElement() method.
- What is noscript tag?
- Difference between '=', '==' and '===' operators?
- What is Browser Object Model
- How to test a string as a literal and as an object ?
- What is Associative Array? How do we use it?
- What's the Difference Between Class and Prototypal Inheritance?