link1 link2//jquery $("#div1 a").clic...">

How can I get the text inside the anchor tag in jquery

<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 the text aside to be the anchor tag that I clicked on it, but clicking on each of the above anchor tags gives me the same answer ("link1link2"), how can I get "link1" when I click on the first tag and get "link2" when I click on the second anchor tag, that anchor tags can be infinite

+4
source share
2 answers

Use this:

  $("#div1 a").click(function(){ var text = $(this).text(); }); 
+4
source

Inside the click handler, this refers to the element that caused the handler to execute:

 $("#div1 a").click(function(){ var text = $(this).text(); }); 
+3
source

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


All Articles