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

166
Vistas
Can't add selector with options to DOM with fetching api

this is my first question here, so I'm little bit nervous ;) My problem is, how to add selector with options to DOM. Main question is that I have api to get data and the value of options should be added from this api. At the beginning I create button to start the function, but then code doesn't work. Here is my code:

const btn = document.createElement("button");
btn.setAttribute("id", "getCurrencies");
btn.innerHTML = "Click me!";
document.body.appendChild(btn);

btn.addEventListener("click", (e) => {
  fetch("https://api.frankfurter.app/latest")
    .then((response) => response.json)
    .then((data) => {
      const selectEL = createElement("select");
      const currencies = Object.entries(data.rates).forEach(([code, value]) => {
        const optionEl = createElement("option");
        // optionEl.setAttribute("value", code);
        optionEl.innerHTML = ($[code], $[value]);
        selectEL.appendChild(optionEl);
      });
      document.body.appendChild(selectEL);
    })
    .catch((err) => console.log(err));
});
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You have multiple mistakes. Start debugging your code piece by piece. If you first look at the console you will get error "ReferenceError: createElement is not defined"

  1. createElement calls for select and option are not going to work, you are missing WHERE in the document to create them. document.createElement works like in the first line of code.

Then you fix those and get the next error "TypeError: Cannot convert undefined or null to object"

  1. You are not calling response.json(). It doesn't do the conversion without calling the function. So it will give typeError.

Then you fix the json call. You will now get error "ReferenceError: $ is not defined" This means $ is not defined and the transpiler has no idea what to do. By looking at your code:

  1. Your innerHTML set is wrong. You could use es6 string optionEl.innerHTML = ${code} - ${value};

Now it starts to work.

  1. I would split the code to functions so it is easier to read.

So start by debugging from the beginning. Remove code lines and read what the console prints for you.

See working example without code splitting: https://codepen.io/shnigi/pen/NWvNwXQ

about 4 years ago · Juan Pablo Isaza Denunciar

0

You should do it like this:

$(document).on('click','#getCurrencies',function(){
 // ...
});

This binds a click event to the document and all child elements within it. This method is referred to as delegated event handling.

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