jQuery HTML Element How to - Add two classes to a paragraph








Question

We would like to know how to add two classes to a paragraph.

Answer


<!--  w w w.j a va2 s.com-->
<html>
  <head>
    <script type="text/javascript" src='http://code.jquery.com/jquery-1.5.2.js'></script>
    <script type="text/javascript">
        $(document).ready(function(){
               $("p:last").addClass("selected highlight");
        });
    </script>
    <style>
      .selected { color:red; }
      .highlight { background:yellow; }
    </style>    
  </head>
  <body>
      <p>A</p>
      <p>B</p>
      <p>C</p>
  </body>
</html>

The code above is rendered as follows: