Retrieve selected option text from a select input

Hi, I have a select component and i need to get both the selected value AND selected option text, but I can’t manage! How do I retrieve the selected option text? Thanks!

How do you populate your component with data?

I get the options (value: international phone code - option: country) from the database

Hello @Carlos_Holguin

I could offer you two solutions.

  1. Create two mirrored objects with the signature {country: number} and {number: country}, and when you want to get a code by country or vice versa, just use these objects.

  2. Set as the value of option not country, but stringified object {value: code, option: coutry} and when you want to get an option text, parse this value to object and get option property from them.

Regards, Dima

Thanks. I’m not sure I know how to implement this. On the other hand, I get the values from a list that I load when the page enters. How can I retrieve the properties of the selected element from that list?

You could iterate over that list and check for a match between the selected value and property from the list.

And how do I retrieve the matching object with all its properties? Or do I have to create a new object with the same properties?

And how do I retrieve the matching object with all its properties?

As I said before, you may iterate over the list of options and compare the selected value and value from the iteration.

Also, I think we have miscommunication. Could you create an example with a minimum logic and describe the expected result?

Basically, I want to create a registration form with a select that populates automatically the “international phone code” when the user selects her country, as in this image:

This is the codeless code that’s working behind:

And I tested the first block of this code as an alternative following your recommendation, but it doesn’t work:

Please help!!! Thanks!

I don’t understand what’s going on in your logic:

If I print, I get this (when onChange):
Uncaught (in promise) TypeError: sourceList is undefined …
No, I don’t see the alert…

Hi @Carlos_Holguin

Could you please create a test page with minimum UI and Logic, just to demonstrate the problem and share a link to the preview with us?

Btw, I assume the country property in the FormData should not be a list, should it?

Regards, Vlad

Hi @vladimir-upirov, right, the country property is the id of the select element, it should not be a list. I managed to remove some redundant logic and use data binding, I’d rather give you the real link:

seems like there are no options in the select

yes, it takes maybe 1 sec to load them (just tried)

I still do not see any options, could you please share how you populate these options?

On the page element:

What does the getDataFromDatabase logic look like?

Actually this comes from a backendless template (car booking)

try to debug it, it seems like your table Country is empty