Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

133
Vistas
Returned options are not seem by selector

I'm writing a function in plain Javascript where I submit some values via AJAX and if it returns results. What is returned is a string with new options for a selector. I need to remove all options from a selector and replace them with the set that's returned.

HTML

<select id="mySelector">
....
</select>

JS

(async () => {
    ....
    const res = await fetch(url, {
        method: "POST",
        headers,
        body
    });

    if (results.ok) {                                
        let myoptions = await results.text();
        var myselectr = document.querySelector("#mySelector");
        myselectr.innerHTML = "";                     
        myselectr.append(myoptions);
    }
})();

It seems to return options and I see them added in DOM but the selector does not seem to see them as options. The thing is I actually need to build the options using my PHP functionality. I think it is because I return it as text string. Not really sure.

What am I missing here?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

append inserts the DOMStrings as Text nodes, causing HTML to be escaped. You should be assigning myoptions to the select's innerHTML instead:

(async () => {
    ....
    const res = await fetch(url, {
        method: "POST",
        headers,
        body
    });

    if (results.ok) {                                
        let myoptions = await results.text();
        var myselectr = document.querySelector("#mySelector");
        myselectr.innerHTML = myoptions;                     

    }
})();
about 4 years ago · Juan Pablo Isaza Denunciar

0

What is the return format after you made the request is it a array.. if its a array then append options manually using a for loop.

var myselectr = document.querySelector("#mySelector");
var option = document.createElement("option");
option.text = "Text";
option.value = "myvalue";
myselectr.append(option);
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda