Remove dynamically added css from <head>

499 views Asked by At

On button click, I'm adding a css to the <head>:

$("head").append("<link id='#color_1_css' href='" + newCssHref +"' type='text/css' rel='stylesheet' />");

which adds to <head>:

<link id="#color_1_css" rel="stylesheet" type="text/css" href="http://domain.com/styles/colors/f69548/f69548.css">

On another button click I need to remove it again. I tried the following, which doesn't work for some reason:

   $('html').on('click', '.clear-color-picker', function(events){
           var id = $(this).attr('data-parentID').replace('background_color','');
           $('#' + $(this).attr('data-parentID')).setColor('');
           $('#' + id).css('background-color','');

           if (id == 'main-color') {
                console.log('clicked');
                $('#color_1_css').remove();
            }
   })

Any ideas?

2

There are 2 answers

0
zord On

Because your id starts with #?

1
Marc B On

Your ID is incorrect:

$("head").append("<link id='#color_1_css'
                            ^---

It should be id='color_1_css', or you use $('##color_1_css') instead