Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

144
Views
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 answers
Answer question

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 Report

0

Have you tried, replace

 errorDiv.innerHTML = para;

with

errorDiv.append(para);
about 4 years ago · Juan Pablo Isaza Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!