Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
133
rated 0 times [  138] [ 5]  / answers: 1 / hits: 18093  / 5 Years ago, thu, september 5, 2019, 12:00:00

I am creating an element like so



const foo = document.createElement('div');
ReactDOM.render(component, foo);


I wonder if it is possible to create foo with an class or id?


More From » reactjs

 Answers
8

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);

[#51691] Thursday, August 29, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
miles

Total Points: 256
Total Questions: 111
Total Answers: 104

Location: Benin
Member since Fri, Mar 24, 2023
1 Year ago
;