views:

27

answers:

1

I have the following anchor tag

<a href="http://www.google.com/"&gt;Google&lt;/a&gt;

I know how to get the href from an anchor take:

alert($(this).attr("href"));

But how do I get the text from the anchor tag, i.e. how do I get "Google"?

+3  A: 

Use .text() for this:

alert($(this).text());

If you wanted the markup (.text() removes tags and such), use .html()

alert($(this).html());

In this case there's no difference, if instead you had this:

<a href="http://www.google.com/"&gt;Google <span>(External)</span></a>

Then there would be:

$(this).text() //"Google (External)"
$(this).html() //"Google <span>(External)</span>"
Nick Craver