How to Remove Style Added with the .css() function Using jQuery

jQuery is a popular JavaScript library that provides a simple and convenient way of manipulating HTML elements on a web page.

One of the key features of jQuery is the ability to easily change the style of an element with the .css() function.

However, there may be times when you need to remove the style added with .css().

In this Javascript tutorial, we will explore how to do this.


Removing a Single Style Property

To remove a single style property, you can use the .css() function with an empty string as the value for that property.

For example, if you have added a background color to an element with the following code:

$("element").css("background-color", "red");

You can remove it by using the following code:

$("element").css("background-color", "");

Removing Multiple Style Properties

If you want to remove multiple style properties, you can pass an object literal to the .css() function, where the properties to be removed are set to an empty string.

For example:

$("element").css({
  "background-color": "",
  "font-size": ""
});

Removing All Styles

To remove all styles added with .css(), you can use the .removeAttr() function and pass in “style” as the argument.

This will remove the “style” attribute from the element, and all styles added with .css() will be removed.

$("element").removeAttr("style");

Conclusion

In conclusion, removing style added with the .css() function in jQuery is straightforward and can be done in a few different ways depending on your requirements.

Whether you want to remove a single property, multiple properties, or all styles, there is a simple and easy solution.

As always, make sure to thoroughly test your code before deploying it to a live website to ensure that it works as expected.

In this tutorial, we learned how to remove styles added with the .css() function in jQuery.

We hope that this information will be useful to you in your future projects.