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

<< ReSharper User tip: Refactor magical strings to variable | Building a Photo Album widget for BlogEngine.NET >>

Applying stylesheets dynamically with jQuery

Saturday, 2 August 2008 21:58 by Rickard Nilsson

Update follow up: jQuery plugin for switching stylesheets

A colleague of mine asked me how to apply a stylesheet to a web page dynamically using jQuery and I had never done such a thing but my first thought was that is must be pretty simple. I've spent a lot of time thinking of so many things other than web et. al. so it was nice to delve into some of that stuff again. Check out the live demo

As we know stylesheets are defined in the head section of an html file like this

<html>
    <head>
        <link rel="stylesheet" href="stylesheet.css" type="text/css" />
    </head>
    <body>
    ...
</html>

Now, say that we want to apply another stylesheet dynamically after the fact, so to speak, triggered by some event. This could be a button click or some other arbitrary event that is triggered. So, what we want to do is simply insert a new <link/> element into the head section of the page DOM. This can be done in a couple of lines of jQuery:

$(document).ready(function () {
    $("a").click(function () {
        $('head').append('<link rel="stylesheet" href="style2.css" type="text/css" />');
    });
});

The key is at what time we add the link to the style sheet. The first line in the code above repeated here asserts that the DOM is ready for manipulation.

$(document).ready(function () {
    //...
});

The second part repeated here adds a click event to all hyperlinks in the page.

$("a").click(function() { //...

And the very task is performed by the last piece of code where the head element is appended with a new link element.

$('head').append('<link rel="stylesheet" href="style2.css" type="text/css" />');
Tags:   jquery, css
Categories:   CSS | JavaScript
Actions:   | Ping backs (4)

Related posts

jQuery plugin for switching stylesheetsThree years ago I wrote a blog post which has become my most visited post ever. To this day it still...Building a Photo Album widget for BlogEngine.NETTutorial on how to implement the first steps of a Photo Album Widget for BlogEngine.NETPodcasts Microsoft .NET developers should followMy advice is that you should listen to .NET Rocks and Hanselminutes podcasts if you work with, or is...

Comments

September 8. 2008 14:56

trackback

Trackback from DotNetKicks.com

Applying stylesheets dynamically with jQuery

DotNetKicks.com

September 9. 2008 10:29

Jonas

Nice example, although I would personally prefer using something like either:

...
$('head > link').filter(':first').attr('href', 'something.css');
...


or

...
$('head > link').filter(':first').replaceWith('<link href="something.css" ... />');
...


Mainly since your code just appends a new <link> element to the DOM every time the link is clicked, without regard to the old one. Of course, these examples imply that a single <link> is used, but using jQuery selectors it would be quite easy to make it a bit "smarter".

Jonas

September 16. 2008 07:38

Rickard

You're right, my attempt was at a very basic level, at best. However, just as intended. I like your first solution in particular but the requirements for the demo was that I needed to apply a second style sheet dynamically. Yours is replacing the first style sheet in the page.

I agree that jQuery selectors makes it a whole lot easier and I'm planning another post with a more 'real-like' implementation.

Rickard

July 9. 2009 07:37

club penguin cheats

Mainly since your code just appends a new <link> element to the DOM every time the link is clicked, without regard to the old one. Of course, these examples imply that a single <link> is used, but using jQuery selectors it would be quite easy to make it a bit "smarter".

club penguin cheats

Comments are closed
 
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