jQuery Style How to - Add two classes to selected tags








Question

We would like to know how to add two classes to selected tags.

Answer


<!--from ww  w.  j  a  va  2 s . c o m-->
<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("one two");
        });
    </script>
    <style>
       .one { color:red; }
       .two { border:1px solid blue; }
    </style>
  </head>
  <body>
      <p>A</p>
      <p>B</p>
      <p>C</p>
  </body>
</html>

The code above is rendered as follows: