0

Here is my code :

 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    ;   "http://www.w3.org/TR/html4/loose.dtd"><html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script type="text/javascript" src="jquery-1.4.2.min.js"></script>
     </head>
    <body>
  <div id="divmenutable"></div>
  <script type="text/javascript">
   $(document).ready(function() {
    var menuTable = $("<table><table>").addClass("table_menu");
    var menuRow = $("<tr></tr>");
    var menuCol = $("<td>awef</td>");
    menuRow.append(menuCol);
    menuTable.append(menuRow);
    $("#divmenutable").append(menuTable);
   });
  </script>
    </body>
</html>

The result, div add table twice. I dont know why. My real code is to use 'for' loop to add data to table. I have minimized my code to show the problem.

1
  • To optimize things, you might want to just put everything into the menuTable selector up front: var menuTable = $("<table class='table_menu'><tr><td>awef</td></tr></table>"); Commented Jul 18, 2010 at 5:12

1 Answer 1

5

You are creating two tables. Close the end tag.

$('<table><table>').length // 2

Versus

$('<table></table>').length // 1

Could also do

$('<table/>')
Sign up to request clarification or add additional context in comments.

2 Comments

A good read about appending in jQuery: learningjquery.com/2009/03/…
Your last table doesn't require the end tag notation. $('<table>') works dandy in jQuery.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.