0

I have a number of dropdowns like this

enter image description here

code:

<label for="schoolType">
  Type of School <span style="color: red">*</span>
</label>
<select class="form-control" id="schoolType" name="schoolType" value="{{user.schoolType}}">
  <option value="none">None</option>
  <option value="public">Public</option>
  <option value="private">Private</option>
  <option value="homeSchool">Home School</option>
</select>

and the database field is like so

enter image description here

What I'm trying to accomplish

This is a select field that is filled out in registration so I want to grab the value from the database and display it in the edit profile so the users can edit their profiles and change the select if need be.

But as the user refreshes I want the option that they select to be in the select as the selected value.

For reference I am using node.js, express and the handlebars templating engine.

1 Answers1

0

To solve an issue like this, I will advise you to fetch the value selected by the user before, store it in a variable and use a ternary operator to activate it. An actual example is given below.

var selectedValue = user.schoolType;

<select class="form-control" id="schoolType" name="schoolType" value="
{{user.schoolType}}">

  <option value="none" selected="{{user.schoolType === 
this.value}}">None</option>
<option value="public" selected="{{user.schoolType === 
this.value}}">Public</option>
<option value="private" selected="{{user.schoolType === 
this.value}}">Private</option>
<option value="homeSchool" selected="{{user.schoolType === 
this.value}}">Home School</option>
 </select>
dealwap
  • 621
  • 2
  • 14
  • 33
  • This is what I'm getting https://gyazo.com/8e4c475a53827718da7fd98f0edc2610 and here is the code now https://gyazo.com/b1fa6b35e71f743b8307db1307a4e5da –  Jan 15 '18 at 15:36
  • Read up on this on how to use ternary operator in handleBar. https://stackoverflow.com/questions/11915839/is-there-a-ternary-operator-in-handlebars-js – dealwap Jan 15 '18 at 16:25
  • If we could get other means of communication, I could look into your code and fix it for you. – dealwap Jan 15 '18 at 16:26
  • I got it fixed :) –  Jan 15 '18 at 17:30