0

this is my html code:

<h3>Ilm selles linnas</h3>
<table>
    <tr>
        <th>id</th>
        <th>city_id</th>
        <th>temp</th>
        <th>date</th>
    </tr>
    <div id="info"></div>
</table>

this is my script:

function(data) {
            $('#info').empty();
            var html = "";
            for(var i = 0; i < data.length; i++) {
                html += "<tr>"+
                    "<td>"+data[i].id+"</td>"+
                    "<td>"+data[i].city_id+"</td>"+
                    "<td>"+data[i].temperature+"</td>"+
                    "<td>"+data[i].date+"</td>"+
                "</tr>";                
            }
            $('#info').append(html);
}

After this is done, in the real ouput this happens:

enter image description here

So:

1) All the info is higher than table headers (<th> tag)

2) All the info isn't in the column, but just smashed together

3) None of the info is showed in HTML source.

What must I change to make it look as it should?

Feel free to ask any questions.

1
  • Can't have a div in the middle of a table like that; append to the table. And it won't be in the HTML source, because it's not on the HTML source--you need to inspect the elements to get the DOM. Commented Sep 23, 2011 at 13:40

4 Answers 4

5

Just remove your <div id="info"></div> and append directly to the table.

$('table tr:gt(0)').remove();

var html = "";
$(data).each(function() {
    html += "<tr>"+
                "<td>"+this.id+"</td>"+
                "<td>"+this.city_id+"</td>"+
                "<td>"+this.temperature+"</td>"+
                "<td>"+this.date+"</td>"+
            "</tr>";    
});   

$('table').append(html);

Code: http://jsfiddle.net/Xdbqs/5/

Sign up to request clarification or add additional context in comments.

Comments

1

You can't put a div in a table. Try this:

<table id="datatable">
<tr>
    <th>id</th>
    <th>city_id</th>
    <th>temp</th>
    <th>date</th>
</tr>
</table>

function(data) {
        var html = "";
        for(var i = 0; i < data.length; i++) {
            html += "<tr>"+
                "<td>"+data[i].id+"</td>"+
                "<td>"+data[i].city_id+"</td>"+
                "<td>"+data[i].temperature+"</td>"+
                "<td>"+data[i].date+"</td>"+
            "</tr>";                
        }
        $('#datatable').append(html);
}

Comments

0
<table id="info">
    <tr>
        <th>id</th>
        <th>city_id</th>
        <th>temp</th>
        <th>date</th>
    </tr>
</table>

As stated it's your markup. Change it to the above.

Comments

0

Don't use a div. You could just use a and your code would work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.