Repeater element set as a variable

Hi,
I have a functioning repeater with text and photo elements.
I now want to set up a text box on the same repeater, which is linked to a variable (townselected). The variable holds the name of one of the columns in my database which the user chooses via the dropdown. (it works fine and drives many things on the page)

I cannot get the text box to work (“#text4”) - it states ‘undefined’. I assume it is because the repeater is trying to find a column called townselected and not a column with the name that contains the contents of the variable (e.g. “Eastbourne”)

Can anyone help me figure out how to get it to work? (btw the reason I am using String in it is because the column holds numbers, not text).

$w.onReady( function () {
$w(‘#resultsRepeater’).onItemReady(($w, itemData, index) => {
$w(“#titleElement”).text = itemData.title;
$w(“#descriptionElement”).text = itemData.description;
$w(“#imageElement”).src = itemData.photo;
$w(“#text4”).text = String(itemData.townselected);

});

So if the townselected holds a number is it a reference field maybe? If it is a reference field you should use it as:

$w("#text4").text = itemData.townselected.title; // If you have used wixData.query and .include("referenced data collection")

If that is not the case try debugging by just console.log(itemData.townselected) and get that to see if there is something in that variable at all.

You could also try the below toString() method because I do not know the String(value) in Wix Code or Javascript as a recommended or working way to convert numbers into String.

$w("#text4").text = itemData.townselected.toString();

I figured this out. I built a function that updated the repeater item based on the content of a variable with this line:

$w(“#displayDistance”).text = (itemData[townselected]).toString();

And I called that line by embedding it into a function that was called when the dropdown changed.