Skip to content
Advertisement

What is the best way to add options to a select from a JavaScript object with jQuery?

What is the best method for adding options to a <select> from a JavaScript object using jQuery?

I’m looking for something that I don’t need a plugin to do, but I would also be interested in the plugins that are out there.

This is what I did:

JavaScript

A clean/simple solution:

This is a cleaned up and simplified version of matdumsa’s:

JavaScript

Changes from matdumsa’s: (1) removed the close tag for the option inside append() and (2) moved the properties/attributes into an map as the second parameter of append().

Advertisement

Answer

The same as other answers, in a jQuery fashion:

JavaScript
Advertisement