?
Solved

JQuery style switcher

Posted on 2011-03-17
3
Medium Priority
?
1,165 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 2000 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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
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).
Suggested Courses

752 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