Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically delete a style from head tag with JQuery

Tags:

jquery

css

styles

I am dynamically creating a style like below and appending that to <head>

 $("<style type='text/css'> .HP{  background-color: "Red "} </style>").appendTo("head");

On the page I want to delete a particular css class and re-add with different value. How this can be done in JQuery.

like image 778
V.B Avatar asked Oct 10 '14 07:10

V.B


2 Answers

You can add an ID to style attribute

 $("<style id='myStyle' type='text/css'> .HP{  background-color: \"Red \"} </style>").appendTo("head");

Then you can simply use ID selector with .remove() like

$('#myStyle').remove()
like image 103
Satpal Avatar answered Oct 04 '22 20:10

Satpal


You can use css(), no need to alter your main stylesheet like,

$('.HP').css('color','red !important');

The above will make the font color red for all elements having HP Class

$('.HP').css('background-color','green !important');// change your bgcolor red to green

And to remove the last added style try this,

$('head style:last').remove();

Also, if you are adding multiple Styles dynamically, then to remove specific Style give a id to style tag and remove() it like,

$('#STYLE-ID-TO-BE-Removed').remove()
like image 26
Rohan Kumar Avatar answered Oct 04 '22 21:10

Rohan Kumar