How to select all links inside the paragraph using jQuery?


jQuery is a popular JavaScript library that simplifies HTML DOM traversal, event handling, and AJAX interactions for rapid web development. It offers a wide range of built-in functions and methods that help developers to manipulate HTML elements, styles, and behaviors dynamically.

In this article, we will see how to select all links inside a paragraph using jQuery. Selecting links inside a paragraph is a common requirement when we want to modify the links in a particular section of our website, such as changing styles, finding links, etc.

How to select all links inside the paragraph?

Selecting links inside the paragraph is a common task and therefore there are multiple ways through which we can select all links inside a paragraph in jQuery. Let’s see some of the different approaches by which we can select links and see how jQuery can do this task effectively and write more efficient and maintainable code.

Approach 1: Using .children() Method

The .filter() method is a method provided by jQuery that allows the users to return all the direct child elements of the selected element. For selecting our links inside the paragraph that contain anchor tags y specifying the tag names, we can use the children method. Below is the syntax for achieving the same.

Syntax

$("p").children("a").each(function() { 
   // add your styles 
});

The syntax given above first selects all the paragraph elements with the help of the ‘$’ function. After this, it calls the children() method on each paragraph element for retrieving all the anchor tags that are descendants of it. Lastly, each() method is used to iterate over each link and select only the anchor i.e., <a> tag to add or change the styles, or to do any other action.

Example

In this example, we have defined a button “btn1” which uses the .children() method to select all anchor tags that are direct children of the paragraph. When the button gets clicked, a jquery code is executed that returns all the direct child elements of the selected element i.e., it returns a green-colored text “Tutorialspoint” from the paragraph in our example.

<html>
   <head>
      <title>Select Links Inside Paragraph Using jQuery</title>
      <script src="https://code.jquery.com/jquery-3.6.3.min.js"></script>
      <script>
         $(document).ready(function(){
            $("#btn2").click(function(){
               $("span").children("a").each(function(){
                  $(this).css("color", "violet");
               });
            });
         });
      </script>
      <style>
         .find-link-class {
            color: black;
            font-weight: bold;
         }
      </style>
   </head>
   <body>
      <p>
         Welcome to <span><a href="http://www.tutorialspoint.com">Tutorialspoint</a></span>. A leading Ed Tech company striving to provide the best 
         learning material on technical and non-technical subjects.
      </p>
      <button id="btn2">Violet Link</button>
   </body>
</html>

Approach 2: Using .filter() Method

The .filter() method is a method provided by jQuery that allows the users to filter the selected elements based on a specific condition. For selecting our links inside the paragraph that contain anchor tags and then retrieving those tags, we have to define the tag name in the filter() method. Below is the syntax for achieving the same.

Syntax

$("p").find("*").filter("a").each(function(){
   // add your styles
});

The syntax given above first selects all the paragraph elements with the help of the ‘$’ function. After this, it calls the find() method on each paragraph element for retrieving all the anchor tags that are descendants of it. Lastly, filter() method is used to iterate over each link using each() method and select only the anchor i.e., tag to add or change the styles, or to do any other action.

Example

In this example, we have defined a button “btn2” which uses the .filter() method to select all anchor tags that are direct children of the paragraph. When the button gets clicked, a jquery code is executed that returns all the links i.e., it returns a violet-colored text “Tutorialspoint” from the paragraph in our example.

<html>
   <head>
      <title>Select Links Inside Paragraph Using jQuery</title>
      <script src="https://code.jquery.com/jquery-3.6.3.min.js"></script>
      <script>
         $(document).ready(function(){
            $("#btn1").click(function(){
               $("p").find("a").each(function(){
                  $(this).css("color", "green");
               });
            });
         });
      </script>
      <style>
         .find-link-class {
            color: black;
            font-weight: bold;
         }
      </style>
   </head>
   <body>
      <p>
         Welcome to <span><a href="http://www.tutorialspoint.com">Tutorialspoint</a></span>. A leading Ed Tech company striving to provide the best 
         learning material on technical and non-technical subjects.
      </p>

      <button id="btn1">Green Link</button>
   </body>
</html>

Approach 3: Using .has() Method

The .has() method is a method provided by jQuery that allows the users to select elements that have a certain descendant element. For selecting our links inside the paragraph that contain anchor tags and then retrieving those tags, we can use this method. Below is the syntax for achieving the same.

Syntax

$("p:has(a)").find("a").each(function(){
   // add your styles
});

The syntax given above first selects all the paragraphs that contain anchor tags with the help of :has() selector. After this, it calls the find() method on each paragraph element for retrieving all the anchor tags that are descendants of it. Lastly, each() method is used to iterate over each link to add or change the styles, or to do any other action.

Example

In this example, we have defined a button “btn3” which uses the .has() method. When the button gets clicked, a jQuery code is executed, and the function selects all anchor tags that have a certain descendant element. i.e., it returns a red-colored text “Tutorialspoint” from the paragraph in our example.

<html>
   <head>
      <title>Select Links Inside Paragraph Using jQuery</title>
      <script src="https://code.jquery.com/jquery-3.6.3.min.js"></script>
      <script>
         $(document).ready(function(){
            $("#btn3").click(function(){
               $("p:has(a)").find("a").each(function(){
                  $(this).css("color", "red");
               });
            });
         });
      </script>
      <style>
         .find-link-class {
            color: black;
            font-weight: bold;
         }
      </style>
   </head>
   <body>
      <p>
         Welcome to <span><a href="http://www.tutorialspoint.com">Tutorialspoint</a></span>. A leading Ed Tech company striving to provide the best 
         learning material on technical and non-technical subjects.
      </p>
      <button id="btn3">Red Link</button>
   </body>
</html>

Conclusion

Selecting links inside a paragraph is a very easy task as it helps in modifying the links of a particular section of our web application. We discussed different ways for selecting all links inside a paragraph using jQuery. As discussed, we learned the three different approaches, namely using the .children() method, .filter() method, and .has() method because all of these ways are efficient and easy to use. Overall, jQuery is a powerful tool that simplifies HTML DOM traversal, event handling, and AJAX interactions for rapid web development.

Updated on: 13-Apr-2023

642 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements