0

i've got another problem with my jquery code. Here some example code:

$(document).on('click', 'input.goto_batterie_pruefung', function() {
   /** some code**/
    $.ajax({
        url: "example.php",
        type: "post",
        data: /* some data */,
        datatype: "json",
        success: function(data) {

            var data=$.parseJSON(data);

            $('#div_testausgabe').html('');
            $('#div_testausgabe').append('<hr /><table><thead><tr><th></th><th></th><th></th><th></th><th></th><th></th><th></th></tr></thead><tbody>');

            $.each(data, function(index, value) {
                  /* some variables for tabledata */

                $('#div_testausgabe').append('<tr>');
                var text='<td>'+data+'</td><td>'+data+'</td><td>'+data+'</td><td>'+data+'</td><td>'+data+'</td><td>'+data+'</td><td>'+data+'</td>';
                $('#div_testausgabe').append(text);
                $('#div_testausgabe').append('</tr>');
            });

            $('#div_testausgabe').append('</tbody></table><hr />');

        }
    });
});

My problem is that the table doesn't get created correctly. the table tags will surround only the thead part.

6
  • Is this your actual code? Try first by changing the comment block to actual code. Change /* some variables for tabledata *" probably to /* some variables for tabledata */ Commented Jan 9, 2014 at 16:21
  • Is the code you're using actually commented out like that? Or did you do that just for the question? Commented Jan 9, 2014 at 16:22
  • Your use of append is wrong: .append('</tr>'); Append() doesn't write string to document but append element. What about reading the DOC? Commented Jan 9, 2014 at 16:24
  • i just commented all the variables out for this question. but the variables shouldn't be the problem, they get loaded correct between the td elements Commented Jan 9, 2014 at 16:24
  • When you use .append() you are supposed to be appending complete DOM nodes. $('#div_testausgabe').append('<tr>'); doesn't do what you think. This will append a <tr> tag, not just the opening tag as you think. You need to build the HTML first, then append the whole string. .append() is not the same as appending to a string you don't append the opening tag, the data, and then the closing tag. You call .append() with the entire HTML string. Commented Jan 9, 2014 at 16:24

3 Answers 3

1

You're definitely building the table wrong. .append() is going to be appending to your <div id="div_testausgabe">, so you end up with

<div> id="div_testausgabe">
   <hr /><table><thead> blah blah blah from your first .append call</table>
   <tr><td>row #1 from the .each() call></tr></td>
   <tr><td>row #2 from the .each() call()</tr></td>
   etc...
</div>

You need to isolate the actual table inside your div, e.g.

$('#div_testausgabe table tbody').append(....);
                    ^^^^^^^^^^^

so that your new tr/td nodes are going into the correct location.

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

Comments

0

Don't append partial html, you will be updating the DOM a lot of times, making it slower and causing multiple redraws and the browser will try to correct it.

You should build a string of html and append that at the end, after the $.each() loop.

Something like:

$('#div_testausgabe').html('');
var table_html = '<hr /><table><thead><tr><th></th><th></th><th></th><th></th><th></th><th></th><th></th></tr></thead><tbody>';

$.each(data, function(index, value) {
    table_html += '<tr><td>'+data+'</td><td>'+data+'</td><td>'+data+'</td><td>'+data+'</td><td>'+data+'</td><td>'+data+'</td><td>'+data+'</td></tr>';
});

table_html += '</tbody></table><hr />';
$('#div_testausgabe').append(table_html);

Comments

0

You are using jQuery's append() as if it was a string concatenation, which it isn't.

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.