I am trying to add in a very simple method of switching between 2 stylesheets.
I can get the stylesheet to fire on click but not able to toggle it back to its origin
This is an alternative solution to consider if, for some reason, Rory's solution cannot be applied. It's ideal to simply toggle a body class and use this class as a base selector - I've recently applied this method for an application switching between a dark and light theme, then storing it to localStorage so that the changes are "remembered" e.g:
Summary
id attribute values) available to use, and since editing core files, to include one, is not considered good practice (for reasons that won't be touched on here) it may be better to store these filepaths in variables);.click() of '#css_toggle', the .each() method is used to
loop through all instances of stylesheets (of which there would most
likely be a few), it also allows us to redefine the scope of
$(this) which will prove helpful moving forward;link currently in scope
has its href attribute stored in a variable;href
attribute of the link element in question is updated accordinglyCode Snippet Demonstration:
var defaultSS = '/wp-content/themes/RIP/assets/css/style.css',
altSS = '/wp-content/themes/RIP/assets/css/style1.css',
hrefAttr;
$('#css_toggle').click(function () {
$('link').each(function(){
hrefAttr = $(this).attr('href');
if (hrefAttr.indexOf(defaultSS) >= 0) {
$(this).attr('href', altSS);
console.log('Was:',hrefAttr);
console.log('Now:',$(this).attr('href'));
} else if (hrefAttr.indexOf(altSS) >= 0) {
$(this).attr('href', defaultSS);
console.log('Was:',hrefAttr);
console.log('Now:',$(this).attr('href'));
}
});
});