I use an inline editor to edit css classes, when a change is made I wish to remove the class definition and add it again, also user has option to delete the element using it so I need to delete the definition.
Adding works using this code:
$("<style>").prop("type", "text/css").html( "#my_element_"+MaxElements+" {"+ xCSSCode +"}").appendTo("head");
however I can't seem to remove this class which is inserted into the head of the page as follows:
<style type="text/css">#my_element_1 {border-radius: 12.5px;
...
}</style>
Adding and removing class dynamically to an element can make a web page more interactive. Using jQuery it becomes a simple and easy task to add and remove class dynamically. It just needs to call a jQuery function with the selector where we want to add a class or remove a class.
To remove a class from an element, you use the remove() method of the classList property of the element.
jQuery Manipulating CSSaddClass() - Adds one or more classes to the selected elements. removeClass() - Removes one or more classes from the selected elements. toggleClass() - Toggles between adding/removing classes from the selected elements. css() - Sets or returns the style attribute.
create a style tag:
var style = $("<style />", {
id : 'myStyleTag',
type: 'text/css',
html: "#my_element_"+MaxElements+" {"+ xCSSCode +"}"
}).appendTo("head");
to remove
style.remove();
// or
$('#myStyleTag').remove();
I would store the elements in an object:
var styles = {};
...
styles[some_identifier] = $("<style>", {
type: "text/css",
html: "#my_element_"+MaxElements+" {"+ xCSSCode +"}"
}).appendTo("head");
You can remove the style tag easily:
styles[some_identifier].remove();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With