Privat I need jQuery to get the privat from the above link. her...">

Get value from tag in jQuery

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

I need jQuery to get the privat from the above link. here i tried.

 $(".Privat").click(function(e) { e.preventDefault(); alert($(this).val()); }); 

but does it return no value? how can i get the value?

+4
source share
5 answers

The <a> tag creates a binding that does not matter (usually only tags that create input). If you need the value of one of its attributes, you can use the .attr() function.

For instance:

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

If you need 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 slightly different and the <a> tag contained other HTML, not just text, for example:

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

Then you can use the .html() function to do this (it will return <span>Privat</span> ). .text() will still return "Private", even if it is wrapped in a gap.

+9
source

to get the attribute value, use the corresponding function:

 $(this).attr('at'); 
+2
source

Try the following:

  alert($(this).attr('at')); 
+2
source

You have a private in many places, but you probably want $(this).html() , which returns the contents of the tag.

+1
source

The .val() method is mainly used to get the values โ€‹โ€‹of form elements such as input , select and textarea this to get the link text:

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

FIDDLE DEMO

+1
source

Source: https://habr.com/ru/post/1481739/


All Articles