javascript - How do I get the text of a tag -


i trying grab text of clicked. have case 2 different links defined under teh same class , need identify of links clicked

<div class="block pdp-estimate-quote">   <ul>     <li>         <a href="#">estimate payment</a>     </li>     <li>         <a href="#">get quote</a>     </li>   </ul> </div> 

and want throw ga event when clicks either link using link text description. following code throws event shows text both links instead of 1 clicked.

$('.pdp-estimate-quote a').click(function(){      var ctatext = $(this).closest(".pdp-estimate-quote").find('a').text();     datalayer.push({ 'event':'event', 'eventcategory': 'get quote', 'eventaction':ctatext, 'eventlabel':{{url path}}  })  }); 

i might missing on question in order text of current element need do

var ctatext = $(this).text(); 

so complete function be

$('.pdp-estimate-quote a').click(function(){      var ctatext = $(this).text();     datalayer.push({ 'event':'event', 'eventcategory': 'get quote', 'eventaction':ctatext, 'eventlabel':{{url path}}  })  }); 

Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

java - Digest auth with Spring Security using javaconfig -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -