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


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>

Updated on: 12-Jun-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements