MyDiv.appendChild(content) // append text content to elementĭ(myDiv) // append element to body Let content = document.createTextNode('This is a text content of an element!') // text content Let myDiv = document.createElement('div') // element creation Syntax with class: ('bg-red','text-lg')Ĭreate element with class using JavaScript We can use the appendChild() method to append an element node to the main body of HTML. Method add() pass over the class if it is already present on the HTML element. We can also add more than one class to an element using that method. We can create an HTML element with class by using classList.add() method. We can assign id, content, and CSS for styling and append that element to the main body of the HTML page.īasic syntax: let myElement = document.createElement(anyTag) We can create any HTML element and attach it to the DOM tree by using this. Create Element in JavaScriptĭocument.createElement() is a default method which accepts parameter of HTML tag name as string and create an HTML element type node. This article will generate an HTML element using a JavaScript function we will call theįunction on the button click event and create an element with class.
0 Comments
Leave a Reply. |