Solved

JQuery style switcher

Posted on 2011-03-17
3
1,115 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
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

This is a how to build your own CSS3 slideshow and when I say CSS3 I mean just CSS with no javascript in sight! There a few examples online of how to do this but most just show you an example without any explanation, others make it more complicat…
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
In this tutorial viewers will learn how to style different bullet points for unordered lists in CSS. Begin with a normal unordered list; the default bullet point is a solid circle: In the CSS, create a defined class of unordered list by typing "ul.S…
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…

786 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