3

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?

3
  • Did it produce any errors? Commented Dec 15, 2014 at 15:50
  • No, it just doesn't remove the style for some reason... Commented Dec 15, 2014 at 15:51
  • <link id="#color_1_css" should be <link id="color_1_css" Commented Dec 15, 2014 at 15:56

2 Answers 2

5

Because your id starts with #?

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

Comments

4

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

1 Comment

"or you use $('##color_1_css') instead" No, you can't, you'd have to escape the second #.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.