Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

70
Visualizações
Comma in variable breaks template literal output

I'm trying to dynamically set options for a <select>. When setting the label for <outgroup>, the string is being broken before the phrase completes. Ex HTML output: <optgroup label="Computers," electronics,="" &&nbsp;internet="" technology=""></optgroup>

It;s supposed to be "Computers, Electronics, & Internet technology"

let select = document.querySelector("#category");
const categoriesURL = "INSERT_URL";
  
let str = "";

  fetch(categoriesURL)
    .then(response => response.json())
    .then(data => {
    data.forEach(category => {
      let categoryTitle = category.title;
      str += `<optgroup label=${categoryTitle}></optgroup>`
      category.subcategories.forEach(sub => {
        let subcategories_id = sub.subcategories_id;
        let subcategoriesURL = `INSERT_URL/${subcategories_id}`;
        fetch(subcategoriesURL)
          .then(response => response.json())
          .then(subData => {
          str += `<option value=${sub.subcategories_id}>` + subData.title + "</option>";
        })
      })
    })
    select.innerHTML = "<option disabled selected>Select a category</option>" + str;
  });

Also, my subcategories aren't populating... Don't know what's wrong here.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Try label="${categoryTitle}" with quotes, because without quotes, it will generate

<optgroup label=Computers, Electronics, & Internet technology>

which is invalid, so the browser adds quotes around each word, and thinks Electronics is another attribute, leading to your problem.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda