Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

adding / removing css Files using JQUERY

Good day!

I want to add and remove CSS files according to the size of the list. My code is as follow:

$("#size_storedList").ready(function(){
    var list_size = $("#size_storedList").attr('value');
    if(list_size <= 4){
        if ($("link").is('.size5')){
            $('link.size5').removeClass();
        }
        if ($("link").is('.size6')){
            $('link.size6').removeClass();
        }
        $('head').append('<link class="size4" rel="stylesheet" href="css/stored_list/list_size4.css" type="text/css" />');
    } else if(list_size == 5){
        if ($("link").is('.size4')){
            $('link.size4').removeClass();
        }
        if ($("link").is('.size6')){
            $('link.size6').removeClass();
        }
        $('head').append('<link class="size5" rel="stylesheet" href="css/stored_list/list_size5.css" type="text/css" />');
    } else if(list_size == 6){
        if ($("link").is('.size4')){
            $('link.size4').removeClass();
        }
        if ($("link").is('.siz5')){
            $('link.size5').removeClass();
        }
        $('head').append('<link class="size6" rel="stylesheet" href="css/stored_list/list_size6.css" type="text/css" />');
    }
});

But it is kind of messy. What can i do to minimize the checking if the file already exists or not so that i can remove it

if ($("link").is('.size5')){
            $('link.size5').removeClass();
}

Thank you.

like image 517
newbie Avatar asked Jan 17 '23 18:01

newbie


1 Answers

<link rel="stylesheet" href="default.css" type="text/css">

<ul>
  <li><a id="css-red" href="#red">Red</a></li>
  <li><a id="css-blue" href="#blue">Blue</a></li>
  <li><a id="css-green" href="#green">Green</a></li>
</ul>

$(document).ready(function() {
  // red
  $("#css-red").click(function() {
    $("link[rel=stylesheet]").attr({href : "red.css"});
  });
});

Above concept is different from you, but I think this will be a good idea. You can customize same to your current code.

like image 148
Sameera Thilakasiri Avatar answered Jan 25 '23 07:01

Sameera Thilakasiri