<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?
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.
to get the value of an attribute use the appropriate function :
$(this).attr('at');
Try this :
alert($(this).attr('at'));
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With