Populating dynamic list using jQuery

I had something like this before in my client side code:

<select name="carlist" class="form-control">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="opel">Opel</option>
  <option value="audi">Audi</option>
</select>

Since all the values are hard coded above, I needed to replace it with the values I am getting from a webservice (JSON response), so I did the following using jQuery and it’s working also:

$.each(data_.cvtermList, function(key, value) {

               $("#carlist").append (

                      "<option>"+value.carName+"</option>"
                                
                )
           });  

So basically the above code is populating the drop down list in the same manner as I had before(hard coded).

I am wondering in my HTML conversion in Javascript which I have done like this "<option>"+value.carName+"</option>", do I need to include value="volvo" also somehow in my javascript code?

Yes, the value will be required if you want a form to submit the value to the server.
Normally the value attribute contains a server-friendly name, with the text being a human-friendly name.

For example:

<option value="1480390611331">Tue Nov 29 2016 16:36:51 GMT+1300</option>

Thanks, I have included it like this since it’s a dynamic value "<option value ="+value.carName+">"+value.carName+"</option>"

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.