Home > Mobile >  Trying to get the name in Tag Manager
Trying to get the name in Tag Manager

Time:09-23

trying to get the element name using jquey, however it is not fetching the name from span tag. i am trying to go into the class id of span, but still can't fetch the name 'Special Offer'.

 jQuery('ul.tools_list > li > a').click(function(){
    var _clickHref = jQuery(this).attr('href');
    var _linkName = jQuery(this).getElement('span.tool_list_text');
    

  
window.dataLayer = window.dataLayer || [];
    window.dataLayer.push({
      'event': 'side_menu_click',
      'carouselHref' : _clickHref,
      'menuName' : _linkName
    });
  });  

CodePudding user response:

getElement() is not a jQuery method/function. Use children() or find() instead. Here, I have used the jQuery's text() method to get the content as text. You can also use html() to get the content as HTML.

jQuery('ul > li > a').click(function(){
    var _clickHref = jQuery(this).attr('href');
    var _linkName = jQuery(this).find('span');
   console.log(_linkName.text());
 });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ul>
  <li>
    <a href="">
      <span>SpecialOffers</span>
    </a>
  </li>
</ul>

  • Related