How to load CSS stylesheet using JavaScript? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to load CSS stylesheet using JavaScript?

Is there a simpler and shorter way to load CSS stylesheet in html using javascript only? I'm planning to make a changeable webpage theme using two different CSS file and I'm going to use JS to load any of the two CSS file.

17th Nov 2019, 2:17 AM
Alfred
Alfred - avatar
1 Answer
+ 2
You could asign an ID to the link element of the CSS file, and then change the href's value via document.getElementById("link-element").setAttribute("href","path-to-css-file");
17th Nov 2019, 4:22 AM
Diego Melo
Diego Melo - avatar