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
Post a Comment