--- title: Option() slug: Web/API/HTMLOptionElement/Option translation_of: Web/API/HTMLOptionElement/Option ---
O construtor Option() cria novos {{domxref("HTMLOptionElement")}}.
var optionElementReference = new Option(text, value, defaultSelected, selected);
text {{optional_inline}}value {{optional_inline}}defaultSelected {{optional_inline}}selected {{optional_inline}}/*assumindo que temos este HTML<select id='s'> </select> */ var s = document.getElementById('s'); var options = [Quatro, Cinco, Seis]; options.forEach(function(elemento, chave) { s.appendChild(new Option(elemento, chave)); }); /*Resultado<select id='s'> <option value="0">Quatro</option> <option value="1">Cinco</option> <option value="2">Seis</option> </select> */
/* assumindo que temos este HTML <select id="s"> <option>Primeiro</option> <option>Segundo</option> <option>Terceiro</option> </select> */ var s = document.getElementById('s');var options = [ 'zero', 'um', 'dois' ];options.forEach(function(elemento, chave) {if (elemento == 'zero') { s[s.options.length] = new Option(elemento, s.options.length, false, false); } if (elemento == 'um') { s[s.options.length] = new Option(elemento, s.options.length, true, false); // Adicionando atributo "selected" } if (elemento == 'dois') { s[s.options.length] = new Option(elemento, s.options.length, false, true); // Apenas irá selecionar a opção na visualização }}); /* Resultado <select id="s"> <option value="0">zero</option> <option value="1" selected="">um</option> <option value="2">dois</option> // O usuário verá esta opção selecionada </select>*/
| Especificação | Status | Comentário |
|---|---|---|
| HTML5 The definition of 'Option' in that specification. |
Recomendado |