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?

2
  • @jason yes, I have everything, I will edit with some more information. Commented Jun 4, 2012 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. Commented Jun 4, 2012 at 22:05

2 Answers 2

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.

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

2 Comments

actually, he is missing more than that. jsfiddle.net/77uLd Here it is working
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)
0

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

$table.append(thead);

and

$table.append(tbody);

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.