1

For switching themes I use the script below which places a cookie and selects a specific CSS file.

if($.cookie("css")) {
    $("link").attr("href",$.cookie("css"));
}
$(document).ready(function() { 
    $("#layout li a").click(function() { 
        $("link").attr("href",$(this).attr('rel'));
        $.cookie("css",$(this).attr('rel'), {expires: 365, path: '/'});
        return false;
    });
});

In combination with these links:

<ul id="layout" class="PanelInfo">
    <li><a href="#" rel="/themes/bootstrap/design/style.css">Default style</a></li>
    <li><a href="#" rel="/themes/bootstrap/design/custom_bootstrap.css">Bootstrap</a></li>
    <li><a href="#" rel="/themes/bootstrap/design/custom_cerulean.css">Cerulean</a></li>
    <li><a href="#" rel="/themes/bootstrap/design/custom_cosmo.css">Cosmo</a></li>
</ul>

The problem is that it loads only one CSS file at all. It should switch only in a couple of CSS files, let's say: style.css, custom_bootstrap.css, custom_cerulean.css and custom_cosmo.css

All other CSS files shouldn't be changed. The situation now is that it disables all other CSS files. How can I achieve this using my script?

2
  • 1
    use .each function Commented Mar 2, 2016 at 19:16
  • What do you mean "All other CSS files shouldn't be changed."? You have multiple link tag I guess? Commented Mar 2, 2016 at 19:37

1 Answer 1

1

It's not completely clear from the question, but I'm guessing you've got several

`<link href="....`

elements on your page? One containing the one where you want to switch the values, and some others which should not be changed?

If so, then I think your problem is that your jQuery selector is selecting ALL the "link" element on your page and amending the URL in all of them to the same thing:

$("link").attr("href",$(this).attr('rel'));

Instead you need to select the specific id of the link you want to change, something like this:

$("#SwitchLink").attr("href",$(this).attr('rel'));

where SwitchLink is the id of specific a <link> element

Sign up to request clarification or add additional context in comments.

5 Comments

I have lots of small css files which are all loaded. but after testing with this code, i noticed that all these small css files are not loaded anymore. So i think the problem is that in the cookie is stored that only the choosen css file (with click on link) must be loaded and no other css files. But the swap should only be between a couple of css files
I think you have the same jQuery problem in your cookie code - it is modifying all "link" elements to have the same href. So again you need to do what I suggested above, but in the if($.cookie("css") block
yes thats correct. I experienced with it and when i change the link to $Switchlink the problem is not anymore, But now the specific style elements do not load anymore when i click them. This is still not working: $("#SwitchLink").attr("href",$(this).attr('rel'));
Have you given the link tag that you want to change the same id as in the jQuery command? (i.e. <link id="SwitchLink"...)
stupid mistake. I was forgotten that. Now it works great!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.