Home > Blockchain >  Javascript: How do I create element with id class and id which is some variable?
Javascript: How do I create element with id class and id which is some variable?

Time:01-31

Javascript: How do I create element with id class and id which is some variable? Where I am not giving ID as some value but ID is equal to some variable

CodePudding user response:

let id="theID", cls="MyClass"; // these are the "variables" ...

const div = document.createElement("div");
div.id=id; div.className=cls;div.textContent="This is the new div."
console.log(div.outerHTML);  // preview of the created markup
document.body.append(div)

CodePudding user response:

Do check DOM Manipulation of JavaScript methods,

document.createElement('tag or div');

document.querySelector('id or class');

document.querySelector('id or class').appendChild(htmlElement);

  •  Tags:  
  • Related