How to display Text when DB value is 1

My Setup.
I have a Select field for Gender which inserts figure 1 into the dB when Male is selected and 2 when Female is selected.

<div class="form-group mb-3">
  <label for="inp_gender" class="form-label col-form-label">Select Gender</label>
  <select id="inp_gender" class="form-select" name="gender" required="">
  <option value="1">Male</option><option value="2">Female</option></select>
</div>

When I pull the data from the DB, I want to display the actual select options (Male and Female) on the front instead of figures 1 and 2. How do I do this? Is there any way to do this using any of the conversion functions?

You could do it with a ternary. It would generate code like this:

dmx-text="(variablename == 1 ? 'Male' : 'Female')"

Great. Thank you.

The tenary Operation worked.
(applicant_data_detail.data.gender == '1' ? 'MALE' : 'FEMALE')

How about if I have more than two options in the Select. Like Job Titles which have numbers as values?

dmx-text="(variablename == 1 ? 'Male' : variablename == 2 ? 'Female' : 'Animal')"

1 Like

Ben has already given you the answer, but as an extra comment:
Like code generally, the way it’s written can make a big difference to its readability. I find ternary operators when nested or with multiple conditions can become very hard to read. Formatting can help a lot, eg:

var1.value == 'pea' ? 'green' 
: var1.value == 'tomato' ? 'red'
: var1.value == 'carrot' ? 'orange'
: 'default value'
4 Likes

Always grateful for the support. Learned something new and I can see the power and usefulness of this :hugs:
@TomD Yours made it clearer. Thank you

2 Likes