vzhen
vzhen

Reputation: 11157

Jquery how to loop data in a specific div

In my page i have a div id="result" and this div already contain data.

Then i have a ajax call to retrieve loop data.

$.each(data, function(name, value) {
                document.write(value.email);
              });
// i tried  .html("#result")  but not working.

How can i place these loop data into the div id="result" and replaced the previous content.

Upvotes: 0

Views: 1089

Answers (4)

Sushanth --
Sushanth --

Reputation: 55750

you are almost close , but you need to build up the html first and then write it to the div content..

var html = '';
$.each(data, function(name, value) {
     html += value.email;
 });

$("#result").empty().append(html);

Upvotes: 1

thecodeparadox
thecodeparadox

Reputation: 87073

var html = '';
$.each(data, function(name, value) {
    html += value.email;
});

$('#result').html( html );

I think, as you're loop over data, it seems you've multiple emeil addressess. So you can make a string of all loop data (email) and then set that as html() to #request, instead of appending to #result within loop again and again.

.html() will replace previous data and update that #request content with new result.

Upvotes: 1

John Kalberer
John Kalberer

Reputation: 5800

You need to append the data to your result div:

var resultElement = $("#result");
$.each(data, function(name, value) {
    resultElement.append(value.email);
});

What you were doing is writing to the document instead of the result div.

Upvotes: 0

Daedalus
Daedalus

Reputation: 7722

Your syntax is incorrect. You want:

$.each(data, function(name, value) {
    $("#result").html(value.email);
});

See the method's documentation

Upvotes: 1

Related Questions