You should use the .setAttribute() method:
g = document.createElement('div');
g.setAttribute("id", "Div1");
Answer from lkaradashkov on Stack Overflowjavascript - Create Element with id or class with React Dom - Stack Overflow
How to create html element using JavaScript and create class/id to it - Stack Overflow
html - Add ID/class to objects from createElement method - Stack Overflow
html - Javascript: How do I create element with id class and id which is some variable? - Stack Overflow
You should use the .setAttribute() method:
g = document.createElement('div');
g.setAttribute("id", "Div1");
You can use g.id = 'desiredId' from your example to set the id of the element you've created.
I would suggest you create it in a proper React way:
const App = () => {
return React.createElement(
"div",
{style:{color:"red"}, id: 'someId', className: "someClass"},
"Here I am",
);
};
ReactDOM.render(React.createElement(App), document.getElementById("root"));
The id and/or class of foo can be specified via the setAttribute(..) and classList.add(..) methods as shown:
const foo = document.createElement('div');
/* Set id of "some-id" on foo */
foo.setAttribute("id", "some-id");
/* Add class of "some-class" to foo */
foo.classList.add("some-class");
You can do it like this:
<script>
let myP = document.createElement("p");
// If you need change CSS you can do it like:
myP.setAttribute("style", "color: red");
myP.id='myP-ID'
myP.className = 'myP-Class'
myP.innerHTML = "SomeText for my P tag";
document.body.appendChild(myP);
</script>
you can add class :
tag.classList.add("gg");
and you can add class and id :
tag.setAttribute("id", "ff");
tag.setAttribute("class", "bb");
One way with Javascript, is by using setAttribute:
element.setAttribute(name, value);
Example:
var btn=document.createElement("BUTTON");
btn.setAttribute("id", "btn_id");
btn.setAttribute("class", "btn_class");
btn.setAttribute("width", "250px");
btn.setAttribute("data-comma-delimited-array", "one,two,three,four");
btn.setAttribute("anything-random", document.getElementsByTagName("img").length);
The advantage of this way is that you can assign arbitrary values to arbitrary names. https://developer.mozilla.org/en-US/docs/Web/API/element.setAttribute
You could assign to its property:
var btn=document.createElement("BUTTON");
btn.id = 'btn_id';
btn.className = 'btn_class';
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)
Run code snippetEdit code snippet Hide Results Copy to answer Expand
Do check DOM Manipulation of JavaScript methods:
document.createElement('tag or div');
document.querySelector('id or class');
document.querySelector('id or class').appendChild(htmlElement);
Do div.classList.add
let div = document.createElement('div');
div.classList.add('test');
let text = document.createTextNode('Test');
div.appendChild(text);
document.body.appendChild(div)
.test {
color: green;
}
Run code snippetEdit code snippet Hide Results Copy to answer Expand
You can also do by className
let div = document.createElement('div');
div.className = 'test';
let text = document.createTextNode('Test');
div.appendChild(text);
document.body.appendChild(div)
.test {
color: red;
}
Run code snippetEdit code snippet Hide Results Copy to answer Expand
You can use Element.classList.add
let div = document.createElement('div');
div.classList.add("myDiv")
I created some code that creates a new p element/tag, like this:
var para = document.createElement("p");
var node = document.createTextNode(txt);
para.appendChild(node);
var element = document.getElementById("div1");
element.appendChild(para);But how can I style this new p? Is there a way to add an id to it, or to style it as it's created? Thanks