[JavaScript] Cargar valores de una función de JS a un select en HTML

HTML:

<form id='miFormulario'>
<select name="provincia" id="provincia">
    <option value="">Seleccione una Provincia...
</select>
</form>

JavaScript:

miformulario = document.querySelector('#miFormulario');
window.addEventListener("load", function() {
        cargarProvincias(event);
    }, false);

//Funcion para cargar las provincias al campo "select".
function cargarProvincias() {
    //Inicializamos el array.
    var array = ;
    //Ordena el array alfabeticamente.
    array.sort();
    //Pasamos a la funcion addOptions(el ID del select, las provincias cargadas en el array).
    addOptions("provincia", array);
}

//Funcion para agregar opciones a un <select>.
function addOptions(domElement, array) {
    var selector = document.getElementsByName(domElement);
    //Recorremos el array.
    for (provincia in array) {
        var opcion = document.createElement("option");
        opcion.text = array;
        selector.add(opcion);
    }
}
PandaGG
Lok'tar ogar

Dejar respuesta

Please enter your comment!
Please enter your name here

Sponsor

Últimos

Themenslots mit Ägypten Motiv

EinführungThemenslots mit Ägypten Motiv sind ein faszinierendes Segment der Online-Spielautomaten, das die reiche Kultur und Geschichte Ägyptens in den Mittelpunkt stellt. Diese Spielautomaten sind...

Yabby Casino Customer Support: How Players Can Get Help with Account and Payment Questions

IntroductionIn the world of online gaming, customer support plays a crucial role, especially for beginners navigating platforms like Yabby Casino. Understanding how to access...

Casino en ligne retrait express : Guide complet pour les parieurs expérimentés

IntroductionLe monde des jeux d'argent en ligne a évolué de manière significative ces dernières années, et le concept de retrait express est devenu un...

Royal Panda Casino Review for Canadian Players

IntroductionRoyal Panda Casino has emerged as a popular online gaming destination for players in Canada. With its user-friendly interface and a wide variety of...

Exploring the Different Gaming Categories Available on Roobet

IntroductionRoobet is an online gaming platform that has gained popularity among players in Canada. It offers a variety of gaming categories that cater to...