0
let example  = document.createElement("p");

and now I want to add a new class to the p (<p>) that I just created.

James Z
  • 12,209
  • 10
  • 24
  • 44

2 Answers2

1

Use the classList and id properties of the element.

let example  = document.createElement("p");
example.id = "newid";
example.classList.add("newclass");
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

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');
Shravan Dhar
  • 1,455
  • 10
  • 18