good day I'm trying to load an array in a select, but I don't know if it's possible to do it the way I do
This is my HTML, with Materialize styles
<div class="input-field col s3" id="SelectVariedad">
<select name="idVariedad" id="idVariedad">
<option></option>
</select>
<label>Variedad</label>
</div>
And this is my js
const variedades = ["100797","111920","09S352","115-135RD","11ST199"]
console.log(variedades);
console.log('Hola mundo');
// const p = document.querySelector('#SelectVariedad');
// p.innerHTML = variedades;
function imprimirVariedades(variedades){
let html = '';
variedades.forEach(variedad => {
html += `
<select name="idVariedad" id="idVariedad">
<option value = "${variedad}">${variedad}</option>
</select>
<label>Variedad</label>
`;
console.log(variedad);
});
const select = document.querySelector('#SelectVariedad');
select.innerHTML = html;
}
There is only an error in the concatenation of the options to form the inner html.
But what if we show a code where we don't have to create the tags
options
, we can create the HTML element and then assign the value of its attributes.