How to remove a CSS attribute with jQuery?

Asked

Viewed 6,309 times

21

In jQuery, it is possible to add an attribute to an element through the function attr. You can also remove an attribute through the function removeAttr.

And when I define an attribute of css through function $.css? How do I remove?

2 answers

24


The simplest solution would be to reset the element:

$.css("background-color", ""); // exemplo com background-color

Example:

$(function(){
  var contador = 0;
  $(".btn").click(function(){
    if(contador == 0)
    {
      $(this).css("background-color", "blue");
      contador = 1;
    }
    else
    {
      $(this).css("background-color", "");
      contador = 0;
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<button class="btn">Exemplo</button>

Source: Stackoverflow

13

Remove a CSS attribute:

$(el).css("color", "");

Remove multiple CSS attributes:

$(el).css({
   "color": "",
   "background-color": "",
   "outline": "",
});

Browser other questions tagged

You are not signed in. Login or sign up in order to post.