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

148
Visualizações
How can I add an element created in Javascript to innerHTML
const totalBill = document.querySelector(".total-bill");
let errorDiv = document.querySelector(".error");

function isValid(e) {
  if (totalBill.value.trim().length == 0) {
    let para = document.createElement("div");
    para.innerText = "Please enter the total amount of bill";
    // para.classList.add("bill-para");
    para.setAttribute('class', 'bill-error')
    console.log(para);
    errorDiv.innerHTML = para;
  }

  e.preventDefault();
}

HTML:

<div class="container">
        <div class="error">
            
        </div>
        <div class="box-container">
            <h1 class="heading">TIP CALCULATOR</h1>
            <form action="">
                <div class="box">
                    <label>How much was your bill?</label><br>
                    <input type="number" placeholder="Total Bill" class="total-bill">
                </div>
            </form>
       </div>  

So I am trying to add a div in div.error section and I want to add div named div.bill-error but whenever I am trying to replace the innerHTML and run the code. It shows me '[Object HTMLDivElement]' instead of "Please enter the total amount of bill". But in the console I see that the div is created. But it is not inserted inside

Can anyone tell me where is the problem and solution. Thanks a lot.

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

0

Use errorDiv.appendChild(para);, or, if you want to delete existing content in errorDiv first, then errorDiv.textContent = ''; errorDiv.appendChild(para);.

about 4 years ago · Juan Pablo Isaza Relatório

0

Have you tried, replace

 errorDiv.innerHTML = para;

with

errorDiv.append(para);
about 4 years ago · Juan Pablo Isaza Relatório

0

use appendChild() , I modified your code, here's the snipppet :

const totalBill = document.querySelector(".total-bill");
let errorDiv = document.querySelector(".error");

function isValid(e) {
  if (totalBill.value.trim().length == 0) {
    let para = document.createElement("div");
    para.innerHTML = "Please enter the total amount of bill";
    // para.classList.add("bill-para");
    para.setAttribute('class', 'bill-error')
    errorDiv.appendChild(para);
  }

}
<div class="container">
        <div class="error">
            
        </div>
        <div class="box-container">
            <h1 class="heading">TIP CALCULATOR</h1>
            <form action="">
                <div class="box">
                    <label>How much was your bill?</label><br>
                    <input type="number" placeholder="Total Bill" class="total-bill">
                </div>
                <button type="button" onclick="isValid();">Check</button>
            </form>
       </div>
       

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