0

in the html i have an empty table tag.

<table cellpadding="5" cellspacing="5" border="0" id="TableID" class='TableOne'>
</table>
<input id="btn" value="test" type="button" />

in a jscript function, i'm adding some initial rows to the table, one of which contains another table.

$('#btn').click(function() {
  var html = '';
  html += "\
    <tr>\
      <td>Row1\
        <table>\
          <tr>\
            <td></td>\
          </tr>\
        </table>\
      </td>\
    </tr>\
    <tr>\
      <td>Row2</td>\
    </tr>";
  $('#TableID').html(html);                   
  var newhtml = "<tr><td>Row3</td></tr>";
  $('#TableID').append(newhtml);
});

...

.tableOne
{
  border: 3px solid black;    
}
.tableTwo
{
  border: 2px solid red;
}

When this code is executed.... instead of seeing

Row1 Row2 Row3

we see

Row1 Row3 Row2

Row 3 is being put within the inner table. Not the table I specified by id.... how can i fix this??


Edit:

I'm not sure what the cause of this actually is, but I found a work around.

<table cellpadding="5" cellspacing="5" border="0" class='tableOne'>
  <tbody id="TableID"></tbody>
</table>
<input id="btn" value="test" type="button" />

...

$('#btn').click(function() {
  var html = '';
  html += "\
    <tr>\
      <td>Row1\
        <table>\
          <tr>\
            <td></td>\
          </tr>\
        </table>\
      </td>\
    </tr>\
    <tr>\
      <td>Row2</td>\
    </tr>";
  $('#TableID').html(html);                   
  var newhtml = "<tr><td>Row3</td></tr>";
  $('#TableID').append(newhtml);
});

..

http://jsfiddle.net/sjord010/U88fa/3/

7
  • Do you close your initial <table> tag? Commented Apr 26, 2011 at 22:45
  • Whipped this up in jsFiddle: jsfiddle.net/JimmySawczuk/bPwMg, is the same problem occurring here? From what I can see, this is expected behavior. Commented Apr 26, 2011 at 22:50
  • Yes, I did close my initial table tag. My code is actually alot more complex than that, that was a very simplified version. Never seen jsFiddle before, good tool. But that is what should be happening... not's what is happening in my actual code. I'll see what i can do to post some of the actual code here Commented Apr 26, 2011 at 22:53
  • I've added quite a bit of the actual code. If there's something missing, let me know. Commented Apr 26, 2011 at 23:05
  • It seems like the only difference between my jsFiddle and your abbreviated example, then, is that I'm using two $.appends instead of one $.append and one $.html. Can you apply that to your actual case? Commented Apr 26, 2011 at 23:39

2 Answers 2

1

Going off of the discussion in the comments, I think $.html just doesn't like when you pass it in malformed HTML. Regardless, here's an updated jsFiddle, and I just used $.append and cleaned up the whitespace a bit. Hope this helps. For posterity's sake, here's the relevant JS code:

var html = '';
html += "<tr><td><table class='tableTwo'><tr><td>Row1</td></tr></table></td></tr>";

html += "<tr><td><table class='tableTwo'><tr><td>Row2</td></tr></table></td></tr>";

$('#TableID').append(html);
newhtml = "<tr><td>Row3</td></tr>";
$('#TableID').append(newhtml);
Sign up to request clarification or add additional context in comments.

1 Comment

Ah! I see! Good to know for future reference. Thanks!
1

Try this out instead of append.

$('#TableID > tr:last').after(newhtml);

This answer has a lot more information about this as well.

Updated fiddle.

1 Comment

This wont' work for me considering the last tr is technically the one that is contained within an inner table, so really it would cause the same issue. I actually tried this.

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.