Rickard Nilsson

  • Blog
  • Archive
  • About
  • Contact

Welcome to rickardnilsson.net

Rickard Nilsson is a software architect, developer, craftsman, agile enthusiast, and father of three... More

Rickard blogs about crafting software using .NET tooling and solid, development practices.

Follow @rickardn

Top Posts

  • Applying stylesheets dynamically with jQuery
  • My customized Son of Obsidian Visual Studio color scheme for ReSharper
  • .NET Development with Visual Studio on MacBook Pro
  • Code Kata Cast
  • ReSharper User tip #2: Refactor rename namespace
  • Combining and minifying JavaScript and CSS files with Ajax Minifier
  • Dependency injection in ASP.NET MVC with Unity IoC Container
  • C# REPL and Interactive interpreter

Categories

  • .NET
  • Agile
  • ASP.NET 2.0
  • ASP.NET 3.5
  • ASP.NET 4
  • ASP.NET MVC
  • BlogEngine.NET
  • C# 2.0
  • C# 3.0
  • C# 4.0
  • Continuous Integration
  • CSS
  • Design by Contract
  • Design Patterns
  • iPhone
  • JavaScript
  • Kata
  • Moles
  • Open source
  • Personal
  • Review
  • Social media
  • Software development
  • TDD
  • Testing
  • Umbraco
  • Unit testing
  • Unity
  • User tip
  • Web development

Five most recent posts

  • How to unit test your database code when using ServiceStack OrmLite
  • Extract class - ReSharper Ninja tricks
  • ASP.NET MVC 3 Template with built in JavaScript and CSS merging and minification
  • ReSharper Ninja tricks - Generate code from usage
  • Unit testing continuously

Tag cloud

  • agile
  • blogengine.net
  • c#
  • code kata
  • codegarden11
  • continuous integration
  • css
  • dependency injection
  • fakes
  • iso 8601
  • javascript
  • jquery
  • refactoring
  • resharper
  • resharper usertip
  • tdd
  • testing
  • umbraco
  • unit test
  • unit testing
  • visual studio

Recent comments

<< rickardnilsson.net on Facebook | Reflections from day 1 - Umbraco Codegarden 11 in Copenhagen >>

jQuery plugin for switching stylesheets

Thursday, 19 May 2011 19:08 by Rickard Nilsson

It has been almost three years since I wrote my top ranking post on this blog: Applying stylesheets dynamically with jQuery. It was a quick and dirty example of a testing scenario pulled together for a colleague but it has become my number one linked post. It is still the post that gets the most hits every month because it is the first hit on Google for jquery add stylesheet which seems to be a lot of people has trouble with.

So, I felt it was time to do another post on the subject considering I’ve been using jQuery more or less every day these last three years.

jQuery Plugin

I’ve created a simple jQuery plugin for adding or switching out stylesheets dynamically and interactively and it can be used like this:

Loading...
  • HTML
  • jQuery hookup
  • jQuery Plugin
<ul>
    <li><a href="#" rel="1.css">Apply Stylesheet 1a>li>
    <li><a href="#" rel="2.css">Apply Stylesheet 2a>li>
    <li><a href="#" rel="3.css">Apply Stylesheet 3a>li>
ul>

 

$('a').click(function () {
    $.stylesheets.clear().add($(this).attr('rel'));
    return false;
});
$.stylesheets = (function () {
    var stylesheets,
        add,
        clear;
 
    add = function (cssfile) {
        $('head').append(''" rel="stylesheet" />');
        return stylesheets;
    };
 
    clear = function () {
        $('head link[rel=stylesheet]').remove();
        return stylesheets;
    };
 
    return stylesheets = {
        add: add,
        clear: clear
    };
} ());

Download source / demo

Tags:   jquery, css, javascript, jquery plugin
Categories:   JavaScript | CSS
Actions:   | Ping backs (1)

Related posts

Applying stylesheets dynamically with jQueryA simple sample of how to add stylesheets dynamically to a page with jQuery.Building a Photo Album widget for BlogEngine.NETTutorial on how to implement the first steps of a Photo Album Widget for BlogEngine.NETASP.NET MVC 3 Template with built in JavaScript and CSS merging and minificationIn a previous post I described the importance of optimizing your web site resources and how you can ...

Comments

May 20. 2011 09:47

trackback

Applying stylesheets dynamically with jQuery

Applying stylesheets dynamically with jQuery

Rickard Nilsson

 
Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© 2008-2011 rickardnilsson.net
Creative Commons-licens