Skip to content
Advertisement

Selecting an option in a template literal based on string

I am creating an admin panel. On this admin panel, Super Admins will be able to edit other admins’ roles. On the edit screen I have a dropdown that contains the available roles an admin can have. I am using a template literal to inject this HTML into a modal of my own design. My issue is, I am not sure how I can dynamically select the option that contains the value of the Admin’s role saved in the database.

For example, if the JSON of admin data is like this { name: John Smith, role: admin } how could I select that in a template literal like this automatically:

JavaScript

I tried a way like this and it does not seem to work:

JavaScript

Advertisement

Answer

You can use the ternary operator.

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement