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,104
  • 10
  • 27
  • 43

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
  • 669,327
  • 51
  • 454
  • 560
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,284
  • 9
  • 17