Solved

JQuery style switcher

Posted on 2011-03-17
3
1,147 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 20

Expert Comment

by:Proculopsis
ID: 35163226

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
ID: 35163832
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
ID: 35174425

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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As a result of several questions about how to use Bootstrap I thought it would be a good idea to write down the development aspect of creating a Bootstrapped website in as little time as possible. Part 1 of this article will only concentrate on g…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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 benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

688 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