Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

add a new row in a table [duplicate]

Possible Duplicate:
Add table row in jQuery

I want to add a new row to my table on a change event. Here is what I have so far:

$('#CourseID').change(function() {
    $('#CourseListTable > tr > td:last').append('<td>...</td>');
});

Here is my table:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<dynamic>" %>    
<table id="CourseListTable">
    <tr>
        <th>Course ID</th>
        <th>Course Section</th>
        <th>Level</th>            
    </tr>
    <tr>
        <td><select name="CourseID" id="CourseID"></select></td>
        <td><select name="CourseSection" id="CourseSection"></select></td>
        <td><select name="Level" id="Level"></select></td>            
    </tr>
</table>

I am not able to get this to work. I am missing something over here can anyone let me know where my error lies?

Thanks in advance.

like image 227
user793468 Avatar asked Jul 18 '12 17:07

user793468


3 Answers

You mention append Row but in your code you are appending just cells.

If you need to actually append a full row, try this:

$('#CourseID').change(function() {
    $('<tr/>').append('<td>...</td>').insertAfter('#CourseListTable tr:last');
});
like image 88
Chandu Avatar answered Oct 20 '22 17:10

Chandu


This line:

$('#CourseListTable > tr > td:last').append('<td>...</td>');

appends a TD (<td>...</td>) to an existing TD (td:last); you want to append it to a TR, eg.

$('#CourseListTable > tr').append('<td>...</td>');

Of course, you mentioned wanting to add a new row, in which case you shouldn't be appending a <td> at all, you should be appending a <tr> (and you should append it to the table, obviously).

like image 6
machineghost Avatar answered Oct 20 '22 17:10

machineghost


$('#CourseID').change(function() {
    $('#CourseListTable > tbody > tr:eq(1)').append('<td>...</td>');
});
like image 3
thecodeparadox Avatar answered Oct 20 '22 19:10

thecodeparadox