I'm new in the CSS businness and I'd like to do something i don't even know if it's the right way to do it.
I want to do a "theme selector" for a website with a preview and when you click, the new theme is applied.
Here is something that looks like what I want:
$(document).on('click', '#theme-selector p', function(e) {
var $target = $(e.target).closest('li');
if($target.length) {
var className = $target.attr('class');
if(className.length) {
$('#main').removeClass().addClass(className);
}
}
});
#theme-selector li {
cursor: pointer;
}
/* Blue */
.theme-blue p {
color: rgba(0, 75, 179, 1);
}
/* Green */
.theme-green p {
color: rgba(0, 154, 51, 1);
}
/* Orange */
.theme-orange p {
color: rgba(233, 122, 70, 1);
}
/* Red */
.theme-red p {
color: rgba(192, 65, 53, 1);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="main" class="theme-blue">
<p>Actual design</p>
<h2>Change your theme</h2>
<ul id="theme-selector">
<li class="theme-blue">
<p>Preview design - Blue</p>
</li>
<li class="theme-green">
<p>Preview design - Green</p>
</li>
<li class="theme-orange">
<p>Preview design - Orange</p>
</li>
<li class="theme-red">
<p>Preview design - Red</p>
</li>
</ul>
</div>
The problem is when I apply a new theme, it also applies to previews (try applying the red theme on jsfiddle and you'll see the problem)
Please, tell me if there is a better way to do it or how to correct this ?
Thank you !