0

I wish to construct a complete table using jQuery.

I try to learn or copycat from this question

Create table with jQuery - append

but no luck.

What I want to create is

<table>
   <thead>
      <tr>                                  
         <th>Problem 5</th>
         <th>Problem 6</th>
      </tr>
   </thead>
   <tbody>
      <tr>
         <td>1</td>
         <td>2</td>
      </tr>
   </tbody>
</table>

The jQuery code I created is

var $table = $('<table/>');

var $thead = $('<thead/>');
$thead.append('<tr>' + '<th>Problem 5</th>' + '<th>Problem 6</th>' + '</tr>';
$table.append(thead);

var $tbody = $('<tbody/>');
$tbody.append( '<tr><td>1</td><td>2</td></tr>' );
$table.append(tbody);

$('#GroupTable').append($table);

But it failed running.

Can anyone tell me why?

Community
  • 1
  • 1
Jackson Tale
  • 25,428
  • 34
  • 149
  • 271
  • @jason yes, I have everything, I will edit with some more information. – Jackson Tale Jun 04 '12 at 21:59
  • the single most important thing you can do right now is learn how to check the error console in a web browser, so that you can help yourself in the future by checking for error messages. – goat Jun 04 '12 at 22:05

2 Answers2

2

You are missing a right paren in your $thead declaration:

$thead.append('<tr>' + '<th>Problem 5</th>' + '<th>Problem 6</th>' + '</tr>'); 
                                                                            ^

and you aren't appending your variables correctly:

$table.append($thead);
              ^

$table.append($tbody);
              ^

Here's a working fiddle.

Jason
  • 51,583
  • 38
  • 133
  • 185
  • actually, he is missing more than that. http://jsfiddle.net/77uLd/ Here it is working – Pablo Mescher Jun 04 '12 at 21:59
  • paren is parenthesis. You should check your syntax before assuming you are doing something wrong. usually ctrl+shift+j will show all syntax errors as they are found (in chrome at least) – Pablo Mescher Jun 04 '12 at 22:00
0

thead, tbody is undefined? $thead, $tbody?

$table.append(thead);

and

$table.append(tbody);
user1419950
  • 326
  • 1
  • 6