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

192
Views
JQuery using .html to past ajax data in table

I have a problem with pasting some html into a div. I think I know what I'm doing wrong but I cannot find a solution to this. I have an empty div called "allbrands". This div is getting filled with data from an POST ajax call after a button has been clicked. I have made a loop that loops through the data, but it replaces the html constantly. So how can i paste this html table without replacing everytime the content?

My div:

<div id="allbrands">

</div>

My Ajax:

<script type="text/javascript">
    $( document ).ready(function()
    {
        $('[name="getbrands"]').click(function (e){
            e.preventDefault();
            $.ajax({
                type: "GET",
                url: '/brands/all',
                success: function (data)
                {
                       for(var i = 0; i < data.length; i++) {
                           $('#allbrands').html('<h6>' + data[i] + '</h6><hr>');
                       }
                }
            });
        });
    });
</script>

The data is filled like this:
enter image description here

about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Change this line:

$('#allbrands').html('<h6>' + data[i] + '</h6><hr>');

to

$('#allbrands').append('<h6>' + data[i] + '</h6><hr>');

and try again.

Explanation: html() replace the old content by adding new one, append() retain old one and append new as well.

about 4 years ago · Santiago Trujillo Report

0

The problem is in your loop. You are overriding the div's value on each iteration. You need to append the value rather than override it:

Try this:

for(var i = 0; i < data.length; i++) {
    $('#allbrands').append('<h6>' + data[i] + '</h6><hr>');
 }
about 4 years ago · Santiago Trujillo Report

0

Please follow below link for more information

http://api.jquery.com/append/

Also add the

$("#allbrands").append();
about 4 years ago · Santiago Trujillo 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!