Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created April 25, 2019 17:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/72253e3b950f5805c331dd45571eb7d1 to your computer and use it in GitHub Desktop.
Save parzibyte/72253e3b950f5805c331dd45571eb7d1 to your computer and use it in GitHub Desktop.
const $select = document.querySelector("#miSelect");
const opcionCambiada = () => {
console.log("Cambio");
};
$select.addEventListener("change", opcionCambiada);
const agregar = () => {
const option = document.createElement('option');
const valor = new Date().getTime();
option.value = valor;
option.text = valor;
$select.appendChild(option);
};
const limpiar = () => {
for (let i = $select.options.length; i >= 0; i--) {
$select.remove(i);
}
};
const mostrar = () => {
const indice = $select.selectedIndex;
if(indice === -1) return; // Esto es cuando no hay elementos
const opcionSeleccionada = $select.options[indice];
alert(`Texto: ${opcionSeleccionada.text}. Valor: ${opcionSeleccionada.value}`);
};
document.addEventListener("DOMContentLoaded", () => {
document.querySelector("#btnAgregar").addEventListener("click", agregar);
document.querySelector("#btnLimpiar").addEventListener("click", limpiar);
document.querySelector("#btnMostrar").addEventListener("click", mostrar);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment