Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add to a css class using JQuery

Tags:

jquery

css

Is it possible to add/update a css class using JQuery.

I know I can add css to a DOM element using this add css rule using jquery, but I would like to add/remove from the css class itself.

like image 378
gotsp Avatar asked May 04 '11 01:05

gotsp


People also ask

How can I add class in jQuery?

jQuery addClass() Method The addClass() method adds one or more class names to the selected elements. This method does not remove existing class attributes, it only adds one or more class names to the class attribute. Tip: To add more than one class, separate the class names with spaces.

How do I add a class in CSS?

If you want to use a class, use a full stop (.) followed by the class name in a style block. Next, use a bracket called a declaration block that contains the property to stylize the element, such as text color or text size. CSS Classes will help you stylize HTML elements quickly.

What is the use of CSS () method in jQuery?

jQuery css() Method The css() method sets or returns one or more style properties for the selected elements. When used to return properties: This method returns the specified CSS property value of the FIRST matched element.


1 Answers

  1. I have best example of how to apply CSS class in multiple tags.
  2. First create simple html page and inside write down your css.
  3. Next you create simple Query and and apply custom css.
  4. Now if you want apply which tag or attributes css using JQuery.
  5. When I write down this code to easily understand

    Code:

    <!DOCTYPE html>
      <html>
        <head>
          <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
        <script>
          $(document).ready(function(){
            $("#btn1").click(function(){
              $("h1,p").toggleClass("blue");
              $("div").toggleClass("important");
            });
          });
          </script>
    <style>
     .important {
        font-weight: bold;
        font-size: 40px;
        color: chartreuse;
     }
    .blue{
        color: #000080;
     }
    </style>
    </head>             
    <body>
      <h1>This is example</h1>
      <p>This is example</p>
      <div>Some important message </div>
      <button id="btn1">Add class </button>
    </body>
    </html>
    
like image 105
hardik Avatar answered Oct 23 '22 18:10

hardik