// main div child of body
const mainDoc = document.getElementById("main"); 

//----------------------------------------------
const newElementDom = (element, where) => {
  // create element you want (div, span, p etc...) here is element parameters
  const newDiv = document.createElement(element);
  
  // add class in your element
  newDiv.classList.add("custom-class");
  
  // add your element as child of where parameters (here is mainDoc variable)
  where.appendChild(newDiv);
  return newDiv;
};
//-------------------------------------------

// i creat div into another div called maindoc
let div1 = newElementDom("div", mainDoc);
// i creat div into another div called maindoc
let div2 = newElementDom("div", mainDoc);
console.log("nex", mainDoc);

// i add custom css into both div
div1.style.cssText = `
width: 500px;
height: 500px;
background: red;
`;
div2.style.cssText = `
width: 500px;
height: 500px;
background: red;
`;
let myElm = document.createElement("p");	// Create a new element

myElm.innerText = 'test';		// Change the text of the element
myElm.style.color = 'red';		// Change the text color of the element

document.body.appendChild(myElm);	// Add the object to the DOMvar newDiv = document.createElement("div");
document.body.appendChild(newDiv);// Create a new element
const newElement = document.createElement("div");

// Set attributes or properties of the element
newElement.id = "myElement";
newElement.className = "my-class";
newElement.textContent = "This is a dynamically created element";

// Append the element to the document body or any other parent element
document.body.appendChild(newElement);const box = document.createElement("div");
box.id = "box";
document.body.appendChild(box);// Create a new <div> element
var newElement = document.createElement("div");

// Set attributes (if needed)
newElement.id = "myElement";
newElement.className = "myClass";

// Set inner HTML (if needed)
newElement.innerHTML = "This is a new element";

// Append the new element to an existing element in the document
document.getElementById("parentElement").appendChild(newElement);

Javascript相关代码片段

react get blue outline on routing Links

get rid of blue button effect on react links

nextjs remote url image

react router v6 basic code

lua table to json online

download video from url javascript

jquery set max length input

JS not selecting the element

react native clock

shadcn toast not showing

express folder structure

audio element

javascript set display of elem to block

react phone number input

tailwind intellisense not working with react

cannot find name 'cy'

submit form react

footer react bootstrap

angular interview questions and answers

trigger alert if button is clicked

change query params

verify control code iban javascript

react-spring

Next JS solve the Hydration error

how can i set a new expo project

generate aes key

storage capacity on browser

nix flake Javascript projects

reset udemy course progress

zod Input file schema with shadcn

wordpress rest api print json pretty

extract string csv js

remove extra space string javascript

count letters in string javascript

exract string js

lazygit nvim

ex:javascript loop

js slice last element

Multiply a number with .17 and round up to .25

sweetalert 2

booking calendar js

process.env is kept

AnimationEvent has no function name specified!

jquery date picker wordpress enque

js check in view

settimeout event in input in javascript in react

node option size

docker react app

React Modal using Dialog

how to create dynamic object in javascript

how to create global variable in postman

javascript interview questions tricky

how to create workflow request in postman

mysql json_extract array

parse response body in postman

query param generator js

jest function async API

React portal

i18next suppoeort react

puppeteer page evaluate

react router dom use navigate

react create array from another array

how to delete global variable in scripts postman

Where does closure variables stored in javascript

Where does local variables stored in javascript

nodejs là gì

addEventListener for multiple selector

js calculate opacity

nodejs create stripe payment from server easy

get child form value in parent viewchild

javascript capitalize every word in a sentence

how to use jquery in moodle

how to handle no internet error in react query

window in not defined in nextjs

cypress find element inside element

app tracking transparency react native permissions

C# Blazor list to json

puppeteer .cache folder not found

css in js

bootstrap next js