Solved

JQuery style switcher

Posted on 2011-03-17
3
1,105 Views
Last Modified: 2012-05-11
Hi,

I'm using the following Jquery styleswitcher - http://www.cssnewbie.com/simple-jquery-stylesheet-switcher/.

The following is my JS reference:
//Stylesheet switcher
	if($.cookie("css")) {
		$("link").attr("href",$.cookie("css"));
	}
	$("#usertools .color a").click(function() { 
		$("link").attr("href",$(this).attr('rel'));
		$.cookie("css",$(this).attr('rel'), {expires: 365, path: '/'});
		return false;
	});

Open in new window


However I don't want it to overwrite a number of embedded style sheets (as its messing up with the admin control panel of my CMS). Can any one advise how I can get this so that it pulls in the selected stylesheet when selected rather than rewrite all embedded stylesheets?

Many thanks.

Webbo
0
Comment
Question by:Webbo_1980
  • 2
3 Comments
 
LVL 20

Expert Comment

by:Proculopsis
Comment Utility

Give the link tag an id and refer to it that way instead of $("link"):

<link id="my-link" rel="stylesheet" type="text/css" media="screen" href="http://www.my.com/style.css" />

$("#my-link").attr("href",...
0
 

Author Comment

by:Webbo_1980
Comment Utility
Hi,

Thanks for this.

II've just tried this, wowever this doesn't appear to allow me to switch the styles when I click on the button. I'm certain this is due to how I'm implementing it - so any guidance would be great!

Here is my HTML:

 
<a href="#" title="Standard colour theme" rel="/css/style.css"><img width="21" height="21" alt="Color 1" src="/images/usertools/colors/color-1.png" /></a> <a href="#" title="black text on a white background" rel="/css/style-2.css"><img width="21" height="21" alt="Color 2" src="/images/usertools/colors/color-2.png" /></a> <a href="#" title="white text on a black background" rel="/css/style-3.css"><img width="21" height="21" alt="Color 3" src="/images/usertools/colors/color-3.png" /></a> <a href="#" title="Black text on a creme background" rel="/css/style-4.css"><img width="21" height="21" alt="Color 4" src="/images/usertools/colors/color-4.png" /></a> <a href="#" title="Black text on a green background" rel="/css/style-5.css"><img width="21" height="21" alt="Color 4" src="/images/usertools/colors/color-5.png" /></a>

Open in new window


Do I need to make any changes to this html as well or not?

Thanks,

Webbo
0
 
LVL 20

Accepted Solution

by:
Proculopsis earned 500 total points
Comment Utility

Does this example help:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_26894822.html</title>
<link id="my-link" rel="stylesheet" type="text/css" href="http://www.experts-exchange.com/css/jsp/generateLink.css" />
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">

jQuery(document).ready( function() {

  $("#usertools .color a").click( function() { 
    $("#my-link").attr("href",$(this).attr('rel'));
//    $.cookie("css",$(this).attr('rel'), {expires: 365, path: '/'});
    return false;
  });


});

</script>
</head>
<body bgcolor="lime">

<div class="toptitle">Click one of the Style Links below</div>


<fieldset id="usertools"><legend>id="usertools"</legend>
  <fieldset class="color"><legend>class="color"</legend>
     <a href="#" rel="http://www.thesitewizard.com/include/style.css">Style One</a>
     <br />
     <a href="#" rel="http://www.thesitewizard.com/include/change-style-sheets/style-alternate.css">Style Two</a>
  </fieldset>
</fieldset>


</body>
</html>

Open in new window

0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

CSS is a visual language used to classify objects and define rules about how they should be displayed. CSS skills aren’t restricted to developers anymore, there is a big benefit to having a basic understanding of the language, regardless of your occ…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
In this tutorial viewers will learn how to style a decorative dropcap for the first letter in a paragraph using CSS. In CSS, create a new paragraph class by typing "p.fancy": Then, to style only the first letter of the first sentence, include the ps…
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now