jQuery Change CSS File Dynamically

Sam Deering
Sam Deering
Share

How to change a css file dynamically using nothing but good old fashion jQuery! (it’s kinda like a jQuery stylesheet switcher!)

The jQuery

$(document).ready(function() {
	$("#nav li a").click(function() {
		$("link").attr("href",$(this).attr('rel'));
		return false;
	});
});
OR (a more complex version to swop the css files dynamically)
$(document).ready(function() {
	if($.cookie("css")) {
		$("link").attr("href",$.cookie("css"));
	}
	$("#nav li a").click(function() {
		$("link").attr("href",$(this).attr('rel'));
		$.cookie("css",$(this).attr('rel'), {expires: 365, path: '/'});
		return false;
	});
});
OR (a version which reduces the flicker which occurs when ths page refreshes with the new css syles after the css is swopped using the relative attribute of the link in the html code).
if($.cookie("css")) {
	$("link").attr("href",$.cookie("css"));
}
$(document).ready(function() {
	$("#nav li a").click(function() {
		$("link").attr("href",$(this).attr('rel'));
		$.cookie("css",$(this).attr('rel'), {expires: 365, path: '/'});
		return false;
	});
});

The HTML

The html is simple with the css and jQuery files loaded as normal in the header of the html.
<link rel="stylesheet" type="text/css" href="style1.css" />
<script type="text/javascript" language="javascript" src="jquery.js"></script>
<script type="text/javascript" language="javascript" src="jquery.cookie.js"></script>
<script>... your jQuery goes here...</script>
Test it out! Here is a couple of links to try it for yourself.
<ul id="nav">
	<li><a href="#" rel="/path/to/style1.css">Default CSS</a></li>
	<li><a href="#" rel="/path/to/style2.css">Larger Text</a></li>
	<li><a href="#" rel="/path/to/style3.css">Something Different</a></li>
</ul>
Source Demo

Frequently Asked Questions (FAQs) about jQuery and CSS

How can I load an external CSS file using jQuery that is compatible with Internet Explorer?

To load an external CSS file using jQuery that is compatible with Internet Explorer, you can use the jQuery’s getScript() method. This method allows you to load JavaScript files dynamically. However, for CSS files, you need to create a new link element and append it to the head of your document. Here’s a simple example:

$('head').append('<link rel="stylesheet" type="text/css" href="your-stylesheet.css">');

This code will add a new link to your CSS file in the head of your document, effectively loading the CSS file.

How can I change the language dynamically in DataTables using jQuery?

DataTables is a powerful jQuery plugin that adds advanced interaction controls to your HTML tables. It supports internationalization so you can change the language dynamically. Here’s an example of how you can do this:

var table = $('#myTable').DataTable({
language: {
url: "//cdn.datatables.net/plug-ins/1.10.21/i18n/English.json"
}
});

// Change language to French
table.language.url = "//cdn.datatables.net/plug-ins/1.10.21/i18n/French.json";
table.ajax.reload(null, false);

This code first initializes a DataTable with English language and then changes the language to French.

How can I use jQuery to change CSS properties?

jQuery provides a method called css() that allows you to get or set style properties. Here’s an example of how you can use this method to change the color of a paragraph:

$('p').css('color', 'red');

This code will change the color of all paragraphs to red.

How can I load CSS and JS files dynamically using jQuery?

You can load CSS and JS files dynamically using jQuery by creating new link and script elements and appending them to the head of your document. Here’s an example:

// Load CSS file
$('head').append('<link rel="stylesheet" type="text/css" href="your-stylesheet.css">');

// Load JS file
$.getScript("your-script.js");

This code will load a CSS file and a JS file dynamically.

How can I use the jQuery-lang-js plugin to manage the language of my website?

The jQuery-lang-js plugin is a lightweight language plugin that allows you to easily translate your website. Here’s an example of how you can use this plugin:

var lang = new Lang('en');
lang.dynamic('fr', 'path/to/fr.json');

// Change language to French
lang.change('fr');

This code first initializes the plugin with English language and then changes the language to French.