I'm in the process of designing a website that will feature two distinct themes: green and blue. By default, the color scheme is set to green, but users will have the option to switch to a blue theme by clicking on a button.
After doing some research on various resources, I came across a code snippet that seems like it could help achieve this functionality:
function updateStyleSheet(filename) {
newstylesheet = "Content/css/" + filename + ".css";
if ($("#dynamic_css").length == 0) {
$("head").append("<link>")
css = $("head").children(":last");
css.attr({
id: "dynamic_css",
rel: "stylesheet",
type: "text/css",
href: newstylesheet
});
} else {
$("#dynamic_css").attr("href", newstylesheet);
}
}
If my understanding is correct, this script should replace the current stylesheet with a new one when called upon. My website has two stylesheets named stylegreen.css
and styleblue.css
. Where in the provided code should I make modifications for it to work seamlessly? Additionally, do I need to include this script within the HTML file and how should I create the button to trigger the theme change?