How to find anchor tag in div and add class using jQuery?

jQueryWeb DevelopmentFront End Technology

To find anchor tag in div, use a selector and the addClass() method adds a class using jQuery.

You can try to run the following code to learn how to find anchor tag in div and add class:

Live Demo

<html>
   <head>
      <script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
      <script>
         $(document).ready(function(){
           $("#button1").click(function(){
             $('#div1 a').addClass('myclass');
           });
        });
      </script>
     
         <style>
          .myclass {
             font-size: 25px;
           }
         </style>

   </head>
   <body>
      <div id="div1">
         <a href="http://www.google.com">Demo</a>
      </div>
      <div id="div2">
         <p>This is demo text.</p>
      </div>
      <button id="button1">Click</button>
   </body>
</html>
raja
Published on 09-Dec-2017 12:08:56
Advertisements