Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how can i get the text inside an anchor tag in jquery

Tags:

jquery

anchor

<div id="div1">
<a herf="#">link1</a>
<a href="#">link2</a>
</div>

//jquery
$("#div1 a").click(function(){
var text = $("#div1 a").text();
});

on the above tags i want to get text in side an anchor tag which i clicked on it but clicking on each of above anchor tag gives me a same answer ('link1link2'), how can i get 'link1' when i click on first a tag and get 'link2' when i click on second anchor tag, be aware that anchor tags can be infinite

like image 244
Farshid Ahmadi Avatar asked Feb 16 '23 06:02

Farshid Ahmadi


1 Answers

Use this :

  $("#div1 a").click(function(){
    var text = $(this).text();
  });
like image 133
Mohammad Arshi Avatar answered May 03 '23 11:05

Mohammad Arshi