Interview QuestionsCategory: HtmlHow we can change our stylesheet path using javascript?
bestonetechnologies Staff asked 5 months ago

How we can change our stylesheet path using javascript?

1 Answers
bestonetechnologies Staff answered 5 months ago

Below is the example to change path of stylesheet:
I have 2 stylesheet (blue.css, green.css) in the same folder in which my html file is.
Code in blue.css:
body{ background:blue; }
Code in green.css:
body{ background:green; }
Main html file Code:
<link type=”text/css” rel=”stylesheet” media=”all” href=”green.css” id=”my_theme_css” />
function changeTheme(theme) {
document.getElementById(‘my_theme_css’).href = theme + ‘.css’;
<p>Testing themes.</p>
<input type=”button” id=”btnTheme” name=”btnTheme” value=”Green” onClick=”changeTheme(‘green’)”/>
<input type=”button” id=”btnTheme” name=”btnTheme” value=”Blue” onClick=”changeTheme(‘blue’)”/>