How to get an element by its href attribute ?

The task is to select the <a> element by its href attribute. Few of the techniques are discussed below. Here we are going to use JQuery to solve the problem.
Approach 1:

  • Use JQuery selector $(‘a[href=link_to_site]’).

Example 1: This example using the approach discussed above.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML>  
<html>  
    <head
        <title
           Get an element by its href attribute.
        </title>
         <script src=
         </script>
    </head
    <body style = "text-align:center;" id = "body">  
        <h1 id = "h1" style = "color:green;" >  
            GeeksforGeeks  
        </h1>
        <p id = "GFG_UP" style = "font-size: 15px; font-weight: bold;">
        </p>
        <a href = "https://www.geeksforgeeks.org">GeeksforGeeks
        </a>
        <br>
        <br>
        <button onclick = "gfg_Run()"
            Click here
        </button>
        <p id = "GFG_DOWN" style = "font-size: 23px; 
           font-weight: bold; color: green; ">
        </p>
        <script>
            var el_up = document.getElementById("GFG_UP");
            var el_down = document.getElementById("GFG_DOWN");
            el_up.innerHTML = 
            "Click on the button to select the element by HREF attribute.";
            function gfg_Run() {
                el_down.innerHTML = 
                       $('a[href="https://www.geeksforgeeks.org"]').text();
            }         
        </script
    </body>  
</html>

chevron_right


Output:



  • Before clicking on the button:
  • After clicking on the button:

Approach 2:

  • Use JQuery selector $(‘a[href*=part_of_link]’). it will select the element if any part of the attribute matches with value.

Example 2: This example using the approach discussed above.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE HTML>  
<html>  
    <head
        <title
           Get an element by its href attribute.
        </title>
         <script src=
         </script>
    </head
    <body style = "text-align:center;" id = "body">  
        <h1 id = "h1" style = "color:green;" >  
            GeeksForGeeks  
        </h1>
        <p id = "GFG_UP" style = "font-size: 15px; font-weight: bold;">
        </p>
        <a href = "https://www.geeksforgeeks.org"> GeeksforGeeks
        </a>
        <br>
        <br>
        <button onclick = "gfg_Run()"
            Click here
        </button>
        <p id = "GFG_DOWN" style = "font-size: 23px; 
           font-weight: bold; color: green; ">
        </p>
        <script>
            var el_up = document.getElementById("GFG_UP");
            var el_down = document.getElementById("GFG_DOWN");
            el_up.innerHTML = 
            "Click on the button to select the element by HREF attribute.";
            function gfg_Run() {
                el_down.innerHTML = $('a[href*="geeks.org"]').text();
            }         
        </script
    </body>  
</html>

chevron_right


Output:

  • Before clicking on the button:
  • After clicking on the button:


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.