ONE_FE
ONE_FE

Reputation: 1006

document.html.className not working in angular2

Following code works fine when I'm removing a class from body tag.

switchTheme(themeCode: string) {
    document.body.className = '';
    document.querySelector('body').classList.add(themeCode);
  }

But I can't remove a class from HTML tag as below.

switchTheme(themeCode: string) {
   document.html.className = '';
   document.querySelector('html').classList.add(themeCode);
}

It gives following error in the first line of the function.

Property 'html' does not exist on type 'Document'.

Any help?

Upvotes: 0

Views: 429

Answers (1)

Nitzan Tomer
Nitzan Tomer

Reputation: 164457

That's because document does not have this html property.
That's not a typescript issue, it's javascript, try to run this in your console:

console.log(document.html);

And you'll get undefined.

To get a reference to the html part of the DOM you need to use the document.documentElement property (the type definition, MDN):

console.log(document.documentElement);

Upvotes: 3

Related Questions