[jquery] How to get anchor text/href on click using jQuery?

Consider I have an anchor which looks like this

 <div class="res">
     <a href="~/Resumes/Resumes1271354404687.docx">
         ~/Resumes/Resumes1271354404687.docx
     </a>
 </div>

NOTE: There won't be any id or class for the anchor...

I want to get either href/text in the jQuery onclick of that anchor.

This question is related to jquery anchor

The answer is


Note: Apply the class info_link to any link you want to get the info from.

<a class="info_link" href="~/Resumes/Resumes1271354404687.docx">
    ~/Resumes/Resumes1271354404687.docx
</a>

For href:

$(function(){
  $('.info_link').click(function(){
    alert($(this).attr('href'));
    // or alert($(this).hash();
  });
});

For Text:

$(function(){
  $('.info_link').click(function(){
    alert($(this).text());
  });
});

.

Update Based On Question Edit

You can get them like this now:

For href:

$(function(){
  $('div.res a').click(function(){
    alert($(this).attr('href'));
    // or alert($(this).hash();
  });
});

For Text:

$(function(){
  $('div.res a').click(function(){
    alert($(this).text());
  });
});

Without jQuery:

You don't need jQuery when it is so simple to do this using pure JavaScript. Here are two options:

  • Method 1 - Retrieve the exact value of the href attribute:

    Select the element and then use the .getAttribute() method.

    This method does not return the full URL, instead it retrieves the exact value of the href attribute.

    _x000D_
    _x000D_
    var anchor = document.querySelector('a'),_x000D_
        url = anchor.getAttribute('href');_x000D_
    _x000D_
    alert(url);
    _x000D_
    <a href="/relative/path.html"></a>
    _x000D_
    _x000D_
    _x000D_


  • Method 2 - Retrieve the full URL path:

    Select the element and then simply access the href property.

    This method returns the full URL path.

    In this case: http://stacksnippets.net/relative/path.html.

    _x000D_
    _x000D_
    var anchor = document.querySelector('a'),_x000D_
        url = anchor.href;_x000D_
    _x000D_
    alert(url);
    _x000D_
    <a href="/relative/path.html"></a>
    _x000D_
    _x000D_
    _x000D_


As your title implies, you want to get the href value on click. Simply select an element, add a click event listener and then return the href value using either of the aforementioned methods.

_x000D_
_x000D_
var anchor = document.querySelector('a'),_x000D_
    button = document.getElementById('getURL'),_x000D_
    url = anchor.href;_x000D_
_x000D_
button.addEventListener('click', function (e) {_x000D_
  alert(url);_x000D_
});
_x000D_
<button id="getURL">Click me!</button>_x000D_
<a href="/relative/path.html"></a>
_x000D_
_x000D_
_x000D_


Updated code

$('a','div.res').click(function(){
  var currentAnchor = $(this);
  alert(currentAnchor.text());
  alert(currentAnchor.attr('href'));
});

Alternative

Using the example from Sarfraz above.

<div class="res">
    <a class="info_link" href="~/Resumes/Resumes1271354404687.docx">
        ~/Resumes/Resumes1271354404687.docx
    </a>
</div>

For href:

$(function(){
  $('.res').on('click', '.info_link', function(){
    alert($(this)[0].href);
  });
});

Edited to reflect update to question

$(document).ready(function() {
    $(".res a").click(function() {
        alert($(this).attr("href"));
    });
});