<\/script>')

Remove dynamically added css from <head>

On button click, I add css to <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 delete 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?

+3


source share


2 answers


Since your ID starts with #

?



+5


source


Your id is wrong:

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

      



It should be id='color_1_css'

, or instead use$('##color_1_css')

+4


source







All Articles