Search code examples
javascriptjquerycss-selectorsgoogle-tag-managergetelementbyid

If this is a function that can tell if the selected element is present in parent element of the clicked element. I need help debugging this


I am making this variable for Google Tag Manager. The {{Click Element}} returns the element lets say "ul". I want to find the parent element with "li" and in the element, I want to search if div.icon-lock is descendant. The current code is not returning the correct result. Can anyone point out the mistake or suggest a better way?

function(){ 
  
  
var ul = {{Click Element}};
var el =( $("ul").closest("li"))

  
if ( $("el div").hasClass("icon-lock") ) { 

   return true
  }
 else {
 
  return false

}   

}

One of the HTML code I am using my function

<li _ngcontent-bvv-c8="" tabindex="0" id="5f2abd67d480440d11d7cec1">
  <div _ngcontent-bvv-c8="" class="cie-accordion-heading">
  <!---->
    <span _ngcontent-bvv-c8="" class="badge-red mr-3">Live Class
    </span>
<!---->Class on Series &amp; Patterns - 1
  </div>
  <div _ngcontent-bvv-c8="" class="cie-accordion-item flex-post">
  <!---->
    <div _ngcontent-bvv-c8="" class="flex-post-pic">
    <!---->
    <img _ngcontent-bvv-c8="" alt="video-img" src="angular/assets/img/course-experience/course-video.svg">
    <!---->
    <!---->
    <!---->
    <!---->
    </div>
 <div _ngcontent-bvv-c8="" class="flex-post-content text-light-sm text-truncate">
 <!---->
 <!---->
 <!---->
  <span _ngcontent-bvv-c8="">81 mins
  </span>
 <!---->
 <!---->
  <span _ngcontent-bvv-c8="" class="px-3">|
  </span>
  <span _ngcontent-bvv-c8="" class="text-light-sm">Was live on Aug 21
  </span>
 </div>
<!---->
 <div _ngcontent-bvv-c8="" class="icon-lock">
 </div>
</div>

Solution

  • Search for the the class by getting the the main div wrapper and then search for the descendant class.

    function hasIconLockClass() {
      const iconLock = document.querySelector('ul li div .icon-lock');
      if (!iconLock) return false
      return true
    }
    
    console.log(hasIconLockClass())
    <ul>
    
    
      <li _ngcontent-bvv-c8="" tabindex="0" id="5f2abd67d480440d11d7cec1">
        <div _ngcontent-bvv-c8="" class="cie-accordion-heading">
          <!---->
          <span _ngcontent-bvv-c8="" class="badge-red mr-3">Live Class
          </span>
          <!---->Class on Series &amp; Patterns - 1
        </div>
        <div _ngcontent-bvv-c8="" class="cie-accordion-item flex-post">
          <!---->
          <div _ngcontent-bvv-c8="" class="flex-post-pic">
            <!---->
            <img _ngcontent-bvv-c8="" alt="video-img" src="angular/assets/img/course-experience/course-video.svg">
            <!---->
            <!---->
            <!---->
            <!---->
          </div>
          <div _ngcontent-bvv-c8="" class="flex-post-content text-light-sm text-truncate">
            <!---->
            <!---->
            <!---->
            <span _ngcontent-bvv-c8="">81 mins
            </span>
            <!---->
            <!---->
            <span _ngcontent-bvv-c8="" class="px-3">|
            </span>
            <span _ngcontent-bvv-c8="" class="text-light-sm">Was live on Aug 21
            </span>
          </div>
          <!---->
          <div _ngcontent-bvv-c8="" class="icon-lock">
          </div>
        </div>
    
      </li>
    
    </ul>