let example = document.createElement("p");
and now I want to add a new class to the p (<p>) that I just created.
let example = document.createElement("p");
and now I want to add a new class to the p (<p>) that I just created.
Use the classList
and id
properties of the element.
let example = document.createElement("p");
example.id = "newid";
example.classList.add("newclass");
You can try it this way:
// create node
const node = document.createElement('p');
// add id
node.setAttribute('id','new_id');
// add class
node.setAttribute('class', 'new-class');