Assign Reference field to a dropdown

Hello there!

I’m looking to assign a collection field of type “Reference” to a dropdown.

The ID of the reference field is 46b40fc8-f433-45ce-aa0a-6c85b7c22bc2

After getting the item, I try to assign the reference value to a dropdown.

Item from collection

{
  "description": "This is a description.",
  "_id": "4d37819d-c259-4ed6-a5bc-d12d7654e9b2",
  "_owner": "bf9f633d-e83d-47ba-b206-08090cfc681e",
  "_createdDate": "Fri Nov 24 2023 12:33:51 GMT-0500 (Eastern Standard Time)",
  "_updatedDate": "Tue Dec 19 2023 17:00:14 GMT-0500 (Eastern Standard Time)",
  "sub-category": "3233922d-c754-46d5-863b-9035403333cd",

}

If I do it like that, my item is getting selected correctly:
$w(‘#sub-category’).value = “3233922d-c754-46d5-863b-9035403333cd” // working

If I assign the dropdown directly from the collection, my item is not selected.
$w(‘#sub-category’).value = String(item.sub-category); // Not working

And Yes, my dropdown is getting populated with all the sub-categories.

What am I doing wrong?

Thanks!

This line is referencing item.category rather than item.sub-category. Worth noting that dashes are not allowed in variable names in Javascript so there’s no way to reference item.sub-category but you could rename it and reference item.subCategory. If this item is coming from a content collection it won’t allow names with dashes anyways.

Also the string primitive (ex. “my string”) and String object are not the same in Javascript: What is the difference between string primitives and String objects in JavaScript? - Stack Overflow not sure if that’s your issue but it’s worth considering.

Looks like I’ll go and review the naming convention. It turns out that sub-category was not acceptable, so subCategory is working just fine.

Thanks Antony

1 Like