views:

71

answers:

3

This is the piece of code I have written:

<html>                                                                  
 <head>                                                                  
 <script type="text/javascript" src="jquery-min.js"></script>          
 <script type="text/javascript">                                         

   $(document).ready(function() {

   var rowIndex = ["A","B","C","D","E","F","G","H","I","J","K","L","N"];
   $.get("crosswords.xml",{},function(xml){

   $('crossword',xml).each(function(i) {

   if("1" == $(this).find("number").text())
   {
          $('body').append("<table border=1>");

          for(var i = 0;i < rowIndex.length;i++)
          {
              $('body').append("<tr height=25>");

              for(var j = 0;j < 13;j++)
              {
                  k = (j+1);
                  var cellname = rowIndex[i] + k;
                  var cell = $(this).find(cellname).text();

                  if(cell == "b")
                  {
                      $('body').append("<td width=25> </td> ");
                  }
                  else if(cell == "c")
                  {
                      $('body').append("<td width=25 bgcolor=black> </td> ");
                  }
                  else
                  {
                     $('body').append("<td width=25>" + cell + "</td> ");
                  }


              }


              $('body').append("</tr> ");
          }

          $('body').append("</table>");
      }

  });
  alert(A);

   });

 })                                     
 </script>                                                               
 </head>                                                                 
 <body>                                                             



 </body>                                                                 
 </html>

In Firefox it is showing as a single column table. In IE7 table is not showing at all. What would be the problem. jQuery version is 1.4

+2  A: 

You need to wrap your width and height values in quotes. Also, instead of appending to the body every single line, create a string variable to which you append the html. Then, at the end, append the entire string.

treeface
Why you would want to construct full html instead of just creating and manipulating DOM nodes is beyond me.
Jamie Wong
@Jamie Because using the append in this way is appreciably faster than manipulating DOM nodes. Using the append method multiple times has the overhead of having to traverse the DOM and parse the string multiple times. See this article: http://www.learningjquery.com/2009/03/43439-reasons-to-use-append-correctly
treeface
+2  A: 

You shouldn't be doing things like this: $('body').append("</tr> ");

When using javascript to create DOM Nodes, you should be creating the entire node, not the opening and closing this. e.g. to build a table you would do something like this:

var table = $('<table>',{border: '1'}).appendTo('body');
for (var i = 0; i < 5; i++) {
    var tr = $('<tr>').appendTo(table);
    for (var j = 0; j < 5; j++) {
        var td = $('<td>').text(i + ',' + j).appendTo(tr);
    }
}
Jamie Wong
Hey Jamie, not sure if you were notified of my response to your comment, but you should take a look at this: http://www.learningjquery.com/2009/03/43439-reasons-to-use-append-correctly In short...creating an html string is significantly faster than manipulating DOM nodes inside a loop.
treeface
If you're concerned with speed at that level, you might as well be using document.write. Also, if you're constructing a massive amount of data, chances are doing it clientside isn't a wise decision in the first place.
Jamie Wong
+1  A: 

Every time you use $('body').append() you append to the document.body, so make it work like:

var $table=$('<table border="1">');

$(document.body).append($table);
...
$table.append('<tr></tr>');
..

and so on.

Zlatev