home

Jquery Css Classes

This methods inspect and manipulate the CSS classes assigned to elements.with using jquery we can remove class, we can add class and also hasclass(has class differentiates whether any of the matched elements are assigned the given class. we can manipulate css on three ways:-

addClass()-adds the class to specified element. here we have button and when you click on button addClass method will add a class(myclass) to p (paragraph) tag.

<script>
      $("button").click(function(){
      $("p").addClass("myclass");
        // addClass will add a myclass to div
    });

//css class
.myclass{
  color:red;
  font-size:12px;
  text-align:center;
  }
</script>

hasClass()-differentiates  whether any of the matched elements are assigned the given class.(read more about https://api.jquery.com/hasClass/).

removeClass()-This can remove the classes it might be single or multiple or selected calss(matched element class).

<script>
     $("div").removeClass("mydivclass")
     // in this case removes div class(mydivclass)
</script>

toggleClass()- Add and remove one or more classes from each element in the set of matched elements.

$(selector).add(element)