Search code examples
jqueryhtmlhref

Get the URL of a link within a div


I want to get the text of the URL that is within a div:

<div class="div">
    <a class="link" href="iwanttogetthis">Link Name</a>
</div>

This is what I have

$('.link').find("href").text();

But it doesn't work.


Solution

  • find searches for elements that are descendants of the selected element.

    text extracts the text nodes from the descendants of the selected element.

    You want to read an attribute value from the selected element. Its descendants are irrelevant.

    $(".link").attr("href");