tags:

views:

677

answers:

3

Consider i have an anchor which looks like this

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

NOTE: There wont be any id or class for the anchor...

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

+3  A: 

Note: Apply the class info_link to any link you want to get the info from. Unlike the answer of rahul, this will only target elements you want to target but what rahul suggested would prompt it up for all the links on your site.

<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());
  });
});
Sarfraz
This will break if another element has the class name link. Better to specify tag selector also.
rahul
@rahul: It is funny, it won't, the `link` class is supposed to be for the specific links he wants to get info from.
Sarfraz
But what if the following markup is there, `<div class='link' />`
rahul
@rahul: it is becoming funny again, he could give his links any unique name.
Sarfraz
@Rahul and @Sarfraz look at my edited part of the question..
Pandiya Chendur
you could use the this.hash instead of $(this).attr('href')
meo
@Pandiya Chendur: please see my updated answer.
Sarfraz
@david: yeah thanks for that, added into answer now.
Sarfraz
Terrible question, in truth, very vague, nothing useful achieved, and also trivial task.
danp
A: 
rahul
that will target all links even the ones he might not be interested in.
Sarfraz
But this is according to his markup in which he hasn't specified a class name.
rahul
@rahul: You should guide him where improvement is possible :)
Sarfraz
Care to explain the reason for down vote..
rahul
@rahul: possible reason for the down vote could be that questioner is not looking for what you have done with `$('a','div.res')`
Sarfraz
+3  A: 

Edited to reflect update to question

$(document).ready(function() {
    $(".res a").click(function() {
        alert($(this).attr("href"));
    });
});
GlenCrawford
that will target all links even the ones he might not be interested in
Sarfraz
@Sarfraz: Cheers, updated
GlenCrawford