Sign in
  • Blog
  • Archive
  • About
  • Contact

Welcome to rickardnilsson.net

rickardnilsson.net is a weblog and the online home of web developer and father of three, Rickard Nilsson... More

Rickard blogs about creating software solutions using ASP.NET and agile practices.

Sites I've visited recently

  • Scott Guthrie
  • Rtur.net
  • ASP.NET Forums
  • Karstad .NET User Group
  • JetBrains ReSharper
  • Scott Hanselman
  • dnrTV!
  • MIX 2009
  • BlogEngine.Net
  • YUI Theater

Categories

  • .NET
  • ASP.NET 2.0
  • BlogEngine.NET
  • C# 2.0
  • C# 3.0
  • CSS
  • Design by Contract
  • Design Patterns
  • JavaScript
  • TDD
  • User tip

Five most recent posts

  • TDD Masterclass in the UK with Roy Osherove
  • ReSharper User tip #2: Refactor rename namespace
  • Building a Photo Album widget for BlogEngine.NET
  • Applying stylesheets dynamically with jQuery
  • ReSharper User tip: Refactor magical strings to variable

Tag cloud

  • ajax
  • blog
  • blogengine.net
  • c#
  • css
  • dbc
  • design by contract
  • dom
  • douglas crockford
  • foto
  • getweekofyear
  • gregoriancalendar
  • highlight
  • html
  • humble dialog box
  • iso 8601
  • javascript
  • jquery
  • jscript
  • julian bucknall
  • metaweblog api
  • model-view-presenter
  • mvp
  • photo album
  • picasa
  • recent posts
  • refactor
  • refactoring
  • release
  • resharper
  • rhino mocks
  • roy osherove
  • syntax
  • syntax highlighter
  • tdd
  • tdd masterclass
  • test coverage
  • types
  • web service
  • week
  • widget
  • word 2007
  • yahoo
  • yui

Recent comments

  • Syntax highlighting in BlogEngine.NET (17)
    Rickard wrote: @Endowme no, its not a regular template. I've desi… [More]
  • Applying stylesheets dynamically with jQuery (9)
    canon t1i wrote: wonderful post [More]
  • Syntax highlighting in BlogEngine.NET (17)
    Naruto Shippuden wrote: that’s good post keep it up .and blog Engine is a … [More]
  • Syntax Highlighter Release 0.2 Beta (23)
    Internet marketing wrote: Try the site on IE6. All content will move to ma… [More]
  • Building a Photo Album widget for BlogEngine.NET (21)
    opal rings wrote: Hey thats Just awesome , Looks Good Just trying f… [More]
<< 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

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 delv 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" />');

5c25bdf1-2101-472d-ac43-4f9582f2455c|4|3.8
Tags:   jquery, css
Categories:   CSS | JavaScript
Actions:   | Permalink | Kick it! | DZone it! | del.icio.us | Comments (9) | Comment RSSRSS comment feed

Related posts

Building a Photo Album widget for BlogEngine.NETTutorial on how to implement the first steps of a Photo Album Widget for BlogEngine.NET

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 8. 2009 22:56

jammer

how r u here?

jammer

July 8. 2009 22:57

Boston movers

Trackback from DotNetKicks.com

Applying stylesheets dynamically with jQuery

Boston movers

July 8. 2009 22:58

Boston movers

need u here with me

Boston movers

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

July 16. 2009 13:51

different types of poker games on line

This looks great. AFAYK are these all 'supported' ways of doing this or is this at all hacky? it looks very promising but i'm worried about the one browser that won't let me do this.

different types of poker games on line

September 3. 2009 14:37

canon t1i

wonderful post

canon t1i

Saving the comment

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading



 
Copyright © 2008-2009 rickardnilsson.net