Create an Element with Class, ID or Style Attribute in Javascript
When working with JavaScript, it is often necessary to dynamically create elements with specific attributes such as class, id, or style. This allows you to tailor the elements to your desired structure and appearance. By programmatically generating elements, you can add interactivity, manipulate the DOM, and enhance the user experience of your web applications. JavaScript provides various methods to create elements and assign attributes to them.
In this article, we will explore different approaches to generating new elements with javascript. You will be able to dynamically generate elements with class, id, or style attributes, empowering you to build more interactive and customized web applications.
Table Of Contents
01. Using "document.createElement()", "element.classList.add()" and "element.setAttribute()"
<!DOCTYPE html> <html> <head> <style> #container{ background-color: #E3483A; padding: 20px; width: 100px; height: 100px; } .myClass { background-color: #EFBA4D; font-size: 18px; width: 100px; height: 100px; } </style> </head> <body> <div id="container"></div> <script> var element = document.createElement("div"); element.classList.add("myClass"); element.id = "myId"; element.setAttribute("style", "color: red;"); document.getElementById("container").appendChild(element); </script> </body> </html>
In this method, a <div>
element is created using document.createElement("div")
. Then, the class is added to the element using element.classList.add("myClass")
, the id is set with element.id = "myId"
, and the style attribute is defined using element.setAttribute("style", "color: red;")
. Finally, the newly created element is appended to an existing element with the id "container" using appendChild()
.
Read Also:
- How To Generate Random Rgb Color Using Javascript
- How To Generate a Random Color in JavaScript
- How To Sort Alphabetically Html Unordered Elements Using JavaScript
- How to Append Text to a DIV using JavaScript
- How to Call a JavaScript Function on Page Load
- How to Get Random Value from Array in Javascript
- How to Get an Object Keys and Values in JavaScript
02. Using "innerHTML" property and template literals
<!DOCTYPE html> <html> <head> <style> #container{ background-color: #E3483A; padding: 20px; width: 100px; height: 100px; } .myClass { background-color: #EFBA4D; font-size: 18px; width: 100px; height: 100px; } </style> </head> <body> <div id="container"></div> <script> var element = document.createElement("div"); element.innerHTML = `<div class="myClass" id="myId" style="color: red;"></div>`; document.getElementById("container").appendChild(element); </script> </body> </html>
In this method, a new <div>
element is created using document.createElement("div")
. The desired HTML code, including the class, id, and style attributes, is assigned to the innerHTML
property using template literals. Finally, the newly created element is appended to an existing element with the id "container" using appendChild()
.
Read Also:
03. Using "insertAdjacentHTML()" method
<!DOCTYPE html> <html> <head> <style> #container{ background-color: #E3483A; padding: 20px; width: 100px; height: 100px; } .myClass { background-color: #EFBA4D; font-size: 18px; width: 100px; height: 100px; } </style> </head> <body> <div id="container"></div> <script> var parentElement = document.getElementById("container"); parentElement.insertAdjacentHTML("beforeend", '<div class="myClass" id="myId" style="color: red;"></div>'); </script> </body> </html>
In this method, an existing parent element with the id "parentElement" is selected using document.getElementById()
. The insertAdjacentHTML()
0is then used to insert the desired HTML code, including the class, id, and style attributes, at the end of the parent element. The "beforeend"
argument ensures that the HTML code is added as the last child of the parent element.
04. Using "setAttribute()" for individual attributes
<!DOCTYPE html> <html> <head> <style> #container{ background-color: #E3483A; padding: 20px; width: 100px; height: 100px; } .myClass { background-color: #EFBA4D; font-size: 18px; width: 100px; height: 100px; } </style> </head> <body> <div id="container"></div> <script> var element = document.createElement("div"); element.setAttribute("class", "myClass"); element.setAttribute("id", "myId"); element.setAttribute("style", "color: red;"); document.getElementById("container").appendChild(element); </script> </body> </html>
In this method, a new <div>
element is created using document.createElement("div")
The class, id, and style attributes are set individually using setAttribute()
The class attribute is assigned using element.setAttribute("class", "myClass")
, the id attribute with element.setAttribute("id", "myId")
, and the style attribute with element.setAttribute("style", "color: red;")
Finally, the newly created element is appended to an existing element with the id "container" using appendChild()
Read Also:
- How to print hello world using javascript
- How to redirect to another page using javascript
- How to refresh page on specific time using javascript
- How to remove a property of JavaScript object
- How to remove a specific item from an array in javascript
- How to scroll to the top of the page using javascript
- How to set default argument values in JavaScript functions
- How to validate an email address Using JavaScript
- What is the reverse of the push function in javascript
- Write a JavaScript function to check if an input is an array
Comments
Post a Comment