Solved

JQuery style switcher

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Regular Expression 1-100 with two digits 15 24
Polygon I google maps 1 16
Materializecss - Different Behaviors in Browser's 1 20
Sorting a string 1 13
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
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…
In this tutorial viewers will learn how to position items using CSS's three positioning types Create a new HTML document with an internal stylesheet.: Create another div in CSS and name it Absolute : Type "position:absolute;" and "top:10px; left:50p…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

895 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

16 Experts available now in Live!

Get 1:1 Help Now