locked
find Hyperlink value using Jquery RRS feed

  • Question

  • User-1120784246 posted

    example 

    <a href="//google.com">click here</a>

    how to find href value //google.com using Jquery( in one line systax). 

    console.log($(...));

    Monday, October 12, 2015 1:18 PM

Answers

  • User61956409 posted

    Hi syedshakeer,

    Would you like to find the value of href attribute? If that is the case, please try to use .attr() method to get the attribute value.

    console.log($("a").attr("href"));

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 12, 2015 10:57 PM
  • User475983607 posted

    syedshakeer

    Your answer is Very close, its not working if my link google.com in 3 rd position. 

    The question is too vague to answer.  The following JQuery absolutely grabs the href attribute of an anchor tab.  

    console.log($("a").attr("href")); 

    However, JQuery can't read your mind.  You have to select the correct anchor element which is what my first post shows how to do.

    This following snippet loops over each anchor element and display the href but you'll need to use an if condition to grab the right element.

        <script type="text/javascript">
            $(document).ready(function () {
                $("a").each(function () {
                    console.log($(this).attr('href'));
                });
            });
        </script>

    What exactly are you trying to do?

    This will grab the 3rd anchor element... Is that what you're after?

        <script type="text/javascript">
            $(document).ready(function () {
                var anchor3 = $("a")[2];
                console.log($(anchor3).attr('href'));
            });
        </script>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 13, 2015 12:15 PM
  • User71929859 posted

    Hi,

    Your answer is Very close, its not working if my link google.com in 3 rd position. 

    console.log($("a").attr("href")); // its returning  1st position link

    Like this

        <script type="text/javascript">
            $(function () {
                console.log($("a").eq(2).prop("href"));
            });
        </script>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 14, 2015 7:12 PM
  • User61956409 posted

    Hi syedshakeer,

    Your answer is Very close, its not working if my link google.com in 3 rd position. 

    console.log($("a").attr("href")); // its returning  1st position link

    The following sample is for your reference.

    <div id="main">
        <a href="myurl1">item1</a>
        <a href="myurl2">item2</a>
        <p>Hello World</p>
        <a href="//google.com">click here</a>
        <a href="myurl3">item3</a>
    </div>
    
    console.log($("#main a:nth-of-type(3)").attr("href"));

    Best Regards,

    Fei Han



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 15, 2015 4:45 AM

All replies

  • User475983607 posted
        <script type="text/javascript">
            $(document).ready(function () {
                console.log($('a[href="//google.com"]'));
                console.log($('a[href="//google.com"]').html());
            });
        </script>

    https://api.jquery.com/attribute-contains-word-selector/

    Monday, October 12, 2015 3:30 PM
  • User61956409 posted

    Hi syedshakeer,

    Would you like to find the value of href attribute? If that is the case, please try to use .attr() method to get the attribute value.

    console.log($("a").attr("href"));

    Best Regards,

    Fei Han

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 12, 2015 10:57 PM
  • User-1461986491 posted
    console.log($("#LinkID").attr("href"));

    For your example:

    console.log($("a").attr("href")); //print "//google.com"



    Tuesday, October 13, 2015 8:24 AM
  • User603616845 posted

    Hi,

    Take an example..

    Html.

    <table class="table1">
        <tbody>        
           <tr class="tablerow1"> 
                <td valign="top"> 
                  <strong> 
                  <a href="http://www.wineweb.com/scripts/wineryPg.cfm/11094/Aan-de-Doorns-Co%2Dop/" target="_top">Aan de Doorns Co-op </a> </strong> </td></tr>
        </tbody>
    </table>

    Jquery

    $('table.table1 tr td a').each(function(idx, elem) {
        console.log( $(this).attr('href'));
    });

    If you want to get from data

    var data = "<a href='http://www.someurl.com'>caption</a>";
    var link = $(data).attr('href'); 

    If you want to get by Id

    console.log($("#MyId").attr("href"));

    Hope this will help you.

    thanks

    Tuesday, October 13, 2015 8:41 AM
  • User-1120784246 posted

    Hi,

    Your answer is Very close, its not working if my link google.com in 3 rd position. 

    console.log($("a").attr("href")); // its returning  1st position link
    Tuesday, October 13, 2015 11:40 AM
  • User475983607 posted

    syedshakeer

    Your answer is Very close, its not working if my link google.com in 3 rd position. 

    The question is too vague to answer.  The following JQuery absolutely grabs the href attribute of an anchor tab.  

    console.log($("a").attr("href")); 

    However, JQuery can't read your mind.  You have to select the correct anchor element which is what my first post shows how to do.

    This following snippet loops over each anchor element and display the href but you'll need to use an if condition to grab the right element.

        <script type="text/javascript">
            $(document).ready(function () {
                $("a").each(function () {
                    console.log($(this).attr('href'));
                });
            });
        </script>

    What exactly are you trying to do?

    This will grab the 3rd anchor element... Is that what you're after?

        <script type="text/javascript">
            $(document).ready(function () {
                var anchor3 = $("a")[2];
                console.log($(anchor3).attr('href'));
            });
        </script>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 13, 2015 12:15 PM
  • User71929859 posted

    Hi,

    Your answer is Very close, its not working if my link google.com in 3 rd position. 

    console.log($("a").attr("href")); // its returning  1st position link

    Like this

        <script type="text/javascript">
            $(function () {
                console.log($("a").eq(2).prop("href"));
            });
        </script>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 14, 2015 7:12 PM
  • User61956409 posted

    Hi syedshakeer,

    Your answer is Very close, its not working if my link google.com in 3 rd position. 

    console.log($("a").attr("href")); // its returning  1st position link

    The following sample is for your reference.

    <div id="main">
        <a href="myurl1">item1</a>
        <a href="myurl2">item2</a>
        <p>Hello World</p>
        <a href="//google.com">click here</a>
        <a href="myurl3">item3</a>
    </div>
    
    console.log($("#main a:nth-of-type(3)").attr("href"));

    Best Regards,

    Fei Han



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 15, 2015 4:45 AM