Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

create dynamic element in javascript

<!DOCTYPE HTML>
<html>

<head>
	<title>
		How to dynamically create
		new elements in JavaScript?
	</title>
</head>

<body>
	<h1 style="text-align:center; color:green;">
		GeeksForGeeks
	</h1>

	<!-- Form to add item -->
	<form action="#" style="text-align:center;">

		<!-- Type of Element -->
		<label for="type">
			Add Element Type
		</label>

		<input type="text" id="type"
			placeholder="Like: div, h1, li...."
			value="li" />
		<br /><br />

		<!-- Text/Value for the element --->
		<label for="value">
			Add Element Value
		</label>

		<input type="text" id="value"
			placeholder="Like: Hello GeeksForGeeks"
			value="CHILD 2" />
		<br /><br />

		<!-- Submit the Form -->
		<button type="button"
			onClick="addItem()">
			Add
		</button>
	</form>

	<!-- Parent tag where we add
		item as child -->
	<ol id="parent">
		<li>List Item 1</li>
	</ol>

	<script>

		// Define the addItem() function
		// to be called through onclick
		function addItem() {

			// Get type of element from form
			let type = document.
				getElementById("type").value;

			// Get the text/value for the tag
			// from the form
			let value = document.
				getElementById("value").value;

			// createElement() is used for
			// creating a new element
			type
				= document.createElement(type);

			// Use value as textnode in this example
			type.appendChild(
				document.createTextNode(value));

			// Append as child to the parent
			// tag i.e. ol
			document.getElementById(
				"parent").appendChild(type);
		}
	</script>
</body>

</html>
Comment

PREVIOUS NEXT
Code Example
Javascript :: compare mongoose id 
Javascript :: import js file 
Javascript :: react router 6 multiple routes layout 
Javascript :: how to show selected value in select box using jquery 
Javascript :: resize array javascript 
Javascript :: print all the subarrays of an array 
Javascript :: javjquery is emptyobject 
Javascript :: bcrypt nodejs hash password 
Javascript :: js countdown 
Javascript :: Divide the number in js 
Javascript :: sequelize array of strings 
Javascript :: convert timestamp to time javascript 
Javascript :: add class with jquery 
Javascript :: js returns the number of true values there are in an array 
Javascript :: react app 
Javascript :: change a variable outside a function js 
Javascript :: .scrollLeft + 1, 0 
Javascript :: javascript push object into array with variable key 
Javascript :: javascript every 
Javascript :: class function 
Javascript :: script src= https//kit.fontawesome.com/a81368914c.js /script 
Javascript :: hourglasses js 
Javascript :: javascript max_value 
Javascript :: how play audio js 
Javascript :: loop over an array 
Javascript :: format to precision 2 javascript if double 
Javascript :: repeat a function javascript 
Javascript :: js match any number string 
Javascript :: square element in array 
Javascript :: Error: contextBridge API can only be used when contextIsolation is enabled 
ADD CONTENT
Topic
Content
Source link
Name
2+4 =