kelvinluck.com

a stroke of luck

jQuery styleswitch – now with toggle!

About three years ago I posted my stylesheet switcher switcher here. It was the first bit of programming I ever did using jQuery. Surprisingly enough it is still one of the most popular posts on my blog and I still get quite frequent questions about it.

One of the most popular questions is “how can you make the styleswitcher toggle between available stylesheets?”. So I just put together a little example of how to do this. While doing it I noticed that the original code wasn’t really that extensible which is probably why people were always asking that question! So I ended up rewriting the code a bit as well as putting together the new example. You can find it all here.

2 Comments, Comment or Ping

  1. Adam Marsh

    This is the ideal script for switching skins. I've tried other methods and Kevin's script is perfect! Thanks for sharing.

    June 17th, 2014

  2. This code does not allow for multiple style sheets with the same title. This is a good way of adding additional features to a page without altering the base sheet. You can extend the script to allow this by replacing the following line in the stylesheetInit function

    availableStylesheets.push(this.getAttribute('title'));

    with the following:

    var sheet = this.getAttribute('title');
    if ( availableStylesheets.indexOf(sheet) < 0 ) {
    availableStylesheets.push(sheet);
    }

    June 26th, 2014