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

147
Views
Jquery append creating issues

Using append my innerdiv is attaching only one WHEAT-COLORED-BOX*; while using appendTo my innerdiv is attaching all the required number of WHEAT-COLORED-BOXES. Thus, for my case appendto is giving right result while append fails to do so!

$(()=>{

    $('button').click(function appendizing(){
        for(let i=1; i<=9; ++i)
             $('<div></div>').addClass('box').css("backgroundColor", "wheat").appendTo('.container');
    });

});
.container{
    font-size: 0px;
    border: 1px solid blue;
    width: 120px;
    height: 120px;
}

.box{
    display: inline-block;
    width: 40px;
    height: 40px;
    border-radius: 10px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container"></div>

<button>appendDiv</button>

now look at this APPEND method usage ->

$(()=>{

    $('button').click(function appendizing(){
    for(let i=1; i<=9; ++i)
$('.container').append('<div</div>').addClass('box').css("backgroundColor", "wheat");
    });

});
.container{
    font-size: 0px;
    border: 1px solid blue;
    width: 120px;
    height: 120px;
}

.box{
    display: inline-block;
    width: 40px;
    height: 40px;
    border-radius: 10px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container"></div>

    <button>appendDiv</button>

What is the reason for this. Kindly explain...

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Check This

$(()=>{

    $('button').click(function appendizing(){
    for(let i=1; i<=9; ++i)
$('.container').append($('<div></div>').addClass('box').css("backgroundColor", "wheat"));
    });

});
.container{
    font-size: 0px;
    border: 1px solid blue;
    width: 120px;
    height: 120px;
}

.box{
    display: inline-block;
    width: 40px;
    height: 40px;
    border-radius: 10px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container"></div>

    <button>appendDiv</button>

about 4 years ago · Juan Pablo Isaza Report

0

  1. You forgot to close the div tag
  2. You are adding class and style to the container, not to the appending div-s. You can specify it inline on append or create element and then append it.

$(()=>{

    $('button').click(function appendizing(){
    for(let i=1; i<=9; ++i)
$('.container').append('<div class="box" style="background-color: wheat;"></div>');
    });

});
.container{
    font-size: 0px;
    border: 1px solid blue;
    width: 120px;
    height: 120px;
}

.box{
    display: inline-block;
    width: 40px;
    height: 40px;
    border-radius: 10px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="container"></div>

    <button>appendDiv</button>

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!