Click here to Skip to main content
15,893,508 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using a select tag in the project to change the theme on the site. that is working like a charm in my project. Here is codepen of my project. jQuery stylesheet switcher[^]
I want to replace select tag with a toggle switch with jquery integration. Like, youtube to switch dark theme. I find many examples of toggle switches that all are based on input tag. I don't know how do I configure it with Jquery.?

What I have tried:

This is my code.

HTML, JS and JQuery

HTML
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select name="" class="style-change form-control" id="style">
  <option value="css/default">Default Style</option>
  <option value="css/bootstrap-slate">Night Mode</option>
</select>


JavaScript
$(function() {
  $('.style-change').change(function() {

    var style = $(this).val();
    $('body').fadeOut("slow", function() {
      $('link[rel="stylesheet"]').attr("href", style + ".css");
      $('body').fadeIn("slow");

      $.cookie("css", style, {
        expires: 365,
        path: '/'
      });
    });
  });
});

$(document).ready(function() {
  $("#style").val('<?php echo $style; ?>');
});


PHP
<?php
if (isset($_COOKIE['css'])) {
    $style = $_COOKIE['css'];
} else {
    $style = 'default';
}
?>



I'm using jQuery cookie library to save user setting in cookies.

Any suggestion would be appreciable.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900