Array of values from javascript to repeating group

function extractAndSetCurrentValues(currentIndex) {
try {
console.log(‘Starting extraction for index:’, currentIndex);

    const valuesString = document.getElementById('diagnosis_values').value;
    console.log('Values string:', valuesString);

    const values = JSON.parse(valuesString);
    console.log('Parsed values:', values);

    let adjustedIndex = currentIndex - 1;
    if (adjustedIndex < 0 || adjustedIndex >= values.length) {
        console.error('Invalid index', currentIndex);
        return;
    }

    const currentValues = values[adjustedIndex];
    console.log('Current Values:', currentValues);

    // Convert array to a simple string to send to Bubble
    const currentValuesString = currentValues.join(',');

    // Log before sending to Bubble
    console.log('Sending to Bubble:', currentValuesString);

    // Use the JavaScript to Bubble function to set the value
    bubble_fn_getCurrentValues(currentValuesString);

    console.log('Successfully set current values in Bubble');
} catch (e) {
    console.error('Error extracting current values:', e);
}

}

// Call the function with the dynamic variable ‘currentIndex’
extractAndSetCurrentValues(currentIndex); // Ensure currentIndex is dynamically provided

I’m running this code and getting these values [‘value1’, ‘value2’, ‘value3’]. but I cannot for the like of me get the values out of the javascript and into the page where I can use them. I’ve tried bringing them into a Input box with an ID attribute and then assigning the inputs value to a custom state. I’ve also just tried referencing the input value directly in my repeating group. What you’re looking at now is my 3rd strategy over the last few hours, and i’m tyring to use JavascriptBubble B but bubble won’t let the string through.
image

Hey @michael39 :wave:

I don’t see the console.log messages in your output. If the code is running, where are those? (maybe they are up a little bit and you didnt screenshot them?)

This can help what are these values passed to the function. And also share your getCurrentValues js2bubble function, so I can give it a look.

Ok - so maybe we should start at the beginning. To be frank, I am not a programmer, and you’re going to think I’m out of my depth here. I generated the following code using chat gpt:

function extractAndSetCurrentValues(currentIndex) {
try {
console.log(‘Starting extraction for index:’, currentIndex);

    const valuesString = document.getElementById('diagnosis_values').value;
    console.log('Values string:', valuesString);

    const values = JSON.parse(valuesString);
    console.log('Parsed values:', values);

    // Adjust index to be zero-based and ensure it is within bounds
    let adjustedIndex = currentIndex - 1;
    if (adjustedIndex < 0 || adjustedIndex >= values.length) {
        console.error('Invalid index', currentIndex);
        return;
    }

    // Extract the values for the current index
    const expression = JSON.stringify(values);
    console.log('Values expression:', expression);

    const regex = /\[((?:"[^"]*",?)*)\]/g;
    let match;
    let matchedValues;
    for (let i = 0; i <= adjustedIndex; i++) {
        match = regex.exec(expression);
        console.log(`Match for index ${i}:`, match);
        if (i === adjustedIndex) {
            matchedValues = match[1];
        }
    }
    console.log('Matched values string:', matchedValues);

    const currentValues = matchedValues.slice(1, -1).split('","');
    console.log('Current Values:', currentValues);

    // Trigger JavaScriptToBubble event
    bubble_fn_currentValues(JSON.stringify(currentValues));

    console.log('Successfully set current values');
} catch (e) {
    console.error('Error extracting current values:', e);
}

}

// Function to trigger JavaScriptToBubble
function bubble_fn_currentValues(values) {
const jsToBubble = document.getElementById(‘jsToBubble_setCurrentValues’);
if (jsToBubble) {
jsToBubble.value = values;
const event = new Event(‘change’);
jsToBubble.dispatchEvent(event);
} else {
console.error(‘JavaScriptToBubble element not found with ID: jsToBubble_setCurrentValues’);
}
}

// Call the function with the dynamic variable
extractAndSetCurrentValues(diagnosis’s currentIndex)); // Ensure currentIndex is within bounds

Here’s the JSON object I’m trying to process:

{
“variable1”: [“value1”, “value2”, “value3”],
“variable2”: [“value4”, “value5”, “value6”]
}

The idea here is that I’m storing multiple variables with an array of value options attached to them. I am populating a list of variables on the page - each with carets next to them to expand the variable and show the available value options.

Right now when I click the caret, i’m getting the array to output from java like so:

Current Values: [‘value1’, ‘value2’, ‘value3’]

or

Current Values: [‘value4’, ‘value5’, ‘value6’]

Now the problem is how do i get current values into bubble to populate the list. How do you recommend i pass this to bubble?

Unfortunately, this is not an easy feat chatGPT can handle especially if you are not a JS programmer a little bit.

Maybe define the original problem of why do you need a custom JS code and maybe it is doable without custom JS and I can help you.

1 Like