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

How we can change our stylesheet path using javascript?

1 Answers
bestonetechnologies Staff answered 7 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:
<html>
<head>
<link type=”text/css” rel=”stylesheet” media=”all” href=”green.css” id=”my_theme_css” />
<script>
function changeTheme(theme) {
document.getElementById(‘my_theme_css’).href = theme + ‘.css’;
}
</script>
</head>
<body>
<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’)”/>
</body>
</html>

Your Answer

2 + 12 =