How to get loop ID in javascript

Hey there,

I have a page with a query loop. For every loop item I need to get the loop id (post/term/user) for further actions…

<script>

  //pop-kategorie
  
  jQuery(document).ready(function(){   
    
    var loopitems = document.querySelectorAll('.loop-slider');
    
    jQuery(loopitems).each(function(){
    
    var currentid = '<?php echo \Bricks\Query::get_loop_object_id(); ?>';
    console.log(currentid);
  
      
    });
  
  });
  
</script>

Unfortunately I only get a 0 for every item.

If you need in javascript you could add attribute for the post-id on your loop-item ,f.e. data-post-id (key) and vor the attribute-value {post_id} . Then you can read your attribute (data-post-id) with javascript.

It’s not a bad idea… I found about the attribute “data-interaction-loop-id” which is added natively in the button which triggers the popup insided the looped item. It’s working and I can’t see any disadvantages going this way so far.

 jQuery(".loop-button").each(function() {
      
   
      jQuery(this).click(function() {
        
        var data = jQuery(this).attr("data-interaction-loop-id");
        var term = data.slice(-2);
       console.log(term);
    	});
    });

I just had a look and it seems that data-interaction-loop-id is added, when there is an interaction applied to the loop item. In a loop without an interaction there’s no such attribute. But in this case you can do it. Glad it works!