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

206
Visualizações
Trying to add a span to an exist div getting error

I am trying to add a span to a exist div , this is the code :

  const x = () => {
    const starsDiv = document.getElementsByClassName("wrapper");
    const span = document.createElement("span");
    let starsArr = [];
    let isHalf = false;
    let numberOfStars;
    for (let i = 0; i < starsDiv.length; i++) {
      starsArr.push(starsDiv[i].children);

      starsArr.map((item) => {
        const length = item.length;
        const starClass = item[item.length - 1].classList.value;
        isHalf = starClass === "fa fa-star-half-o" ? true : false;
        numberOfStars = isHalf ? length + 0.5 : length;
      });
      span.innerHTML = numberOfStars;
      starsDiv.appendChild(span);
    }
  };

I get this error: Uncaught TypeError: starsDiv.appendChild is not a function hope you can help me figure my problem! thx

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

0

The problem is that starsDiv is not a single element. As per your code here:

const starsDiv = document.getElementsByClassName("wrapper");

getElementsByClassName returns an HTMLCollection - even if there is only one element retrieved: https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByClassName

From here you need to append the span to a single element. Which element will depend on your HTML structure and where you want the span to appear. appendChild will make it appear as the last child of an element.

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