Forum Academy Marketplace Showcase Pricing Features

Update Function in Repeating Group - Capture Element ID

Hi! Hoping for some help / clarification on this issues I’m facing.

I have a plugin element with a single field: Element ID. This plugin is in a repeating group and captures the element ID of an input in the same cell of the repeating group. I have set both the element ID of the input and the plugin field to the parent thing’s unique ID. Now, if the order of the repeating group changes (i.e. the cells are re-ordered or a new cell is inserted in the middle), the plugin field should still reflect the unique ID of the parent, and thus the element ID of the input.

To test this, I added some code to the update function.

var element = document.getElementById(properties.elementId)
console.log(element)

This should run (1) when the repeating group is loaded initially and (2) any time the element ID changes. In this case, when I shift the order of the repeating group, the update function should run twice: one for each of the two cells whose unique ID changed after switching positions in the repeating group. Here’s what I get in the console:

One of the two results is null, I assume because the element is in the process of switching and thus may not yet be visible in the repeating group. So, I updated the code to wait until the element is visible:

function checkElement () {
 
    var element = document.getElementById(properties.elementId)
    
    if (!element) {window.setTimeout(checkElement, 250)}
    
    else if (element) {console.log(element)}
    
}

checkElement()

Back in the console, I now get two elements. In actuality, these are the same element, printed twice (check the id of the element below). I’m looking to capture two different elements.

I then updated my else if statement to capture the element id:

else if (element) {
        
        console.log(element)
        console.log("Element ID: " + element.id)
    
    }

Curiously, this is the result I get. Why would the same element be printed twice, but the IDs be different? Shouldn’t the element.id command be pulling directly from the element? If so, how could the same element produce two different IDs? Ultimately, I want to capture both elements (inputs) when the repeating group is reordered - so I want two different elements with two different IDs.

Why don’t you use an observer. You will get better results