Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get value from a tag in jQuery

Tags:

jquery

<a href="?at=privat" at="privat" class="Privat">Privat</a>

I need a Jquery to get the privat from above link. here i Tried .

$(".Privat").click(function(e) {
    e.preventDefault();


     alert($(this).val());
});

but it didn't returns any value? how can i get the value?

like image 827
Arun Avatar asked May 20 '13 10:05

Arun


4 Answers

The <a> tag creates an anchor, which doesn't have a value (generally only tags that create inputs do). If you want the value of one of its attributes, then you can use the .attr() function.

For example:

alert($(this).attr('at')); // alerts "privat"

If you want the value of its text (the content between the <a> and </a> tags), you can use the .text() function:

alert($(this).text()); // alerts "Privat"

If your HTML was a bit different, and your <a> tag contained other HTML, rather than just text, like this:

<a href="?at=privat" at="privat" class="Privat"><span>Privat</span></a>

Then you could use the .html() function to do that (it would return <span>Privat</span>). The .text() would still just return "Privat" even though it's wrapped in a span.

like image 68
Anthony Grist Avatar answered Oct 06 '22 01:10

Anthony Grist


to get the value of an attribute use the appropriate function :

$(this).attr('at');
like image 21
Nick Andriopoulos Avatar answered Oct 06 '22 02:10

Nick Andriopoulos


Try this :

   alert($(this).attr('at'));
like image 36
jasse Avatar answered Oct 06 '22 02:10

jasse


The .val() method is primarily used to get the values of form elements such as input, select and textarea. Try this for getting the link text:

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

FIDDLE DEMO

like image 23
palaѕн Avatar answered Oct 06 '22 01:10

palaѕн