Append a td to the first row of a table Object with JQuery

I know that this should be super easy but I haven't figured it out yet. I'd like to append a < td> to the first row of my table. I tried with this:

$table.get('tr:first').append("<td>Well "+(wellN)+"</td>");

But it didn't work. I also used this:

$($table +' tr:first').append("<td>Well "+(wellN)+"</td>");

But as expected, I got the following error: Uncaught Syntax error, unrecognized expression: [object Object]

Everything else is working right with my table Object. Thanks in advance!

Answers


You want to use .find() as .get() only takes in integers as a parameter:

From the docs:

A zero-based integer indicating which element to retrieve.

So change out .get() for .find():

$table.find('tr:first').append("<td>Well "+(wellN)+"</td>");

This assumes that $table is a jQuery object of the table element you want to alter. E.g. $table = $('table');

Here is a jsfiddle of the above solution: http://jsfiddle.net/jasper/SRsnq/


Here is a working example on jsFiddle

HTML

<table>
    <tr>
        <td>1</td>
        <td>2</td>
        <td>3</td>
    </tr>
    <tr>
        <td>1</td>
        <td>2</td>
        <td>3</td>
    </tr>
    <tr>
        <td>1</td>
        <td>2</td>
        <td>3</td>
    </tr>
</table>    
<a>click me</a>

JAVASCRIPT

$('a').click(function() {
    $('table').find('tr:first').append('<td>Works!</td>');
});

try the following:

$table.find('tr:first').append("<td>Well "+(wellN)+"</td>");

try

$table.find('tr').first().append('<td>:)</td>')

more info


Something like this should work:

$("#mytable tr:first").append($("<td>").text("Foo"));

Need Your Help

Seeding a Database with Code First Entity Framework - Foreign key syntax

entity-framework code-first seeding

I am trying to find the correct syntax to seed a database with test data. I have a foreign key to my product table. It is the category. I have seeded the database with the values for categories, but

Plotting coordinates of multiple points at google map in R

r google-maps plot ggplot2 ggmap

I wanted to plot the coordinates on a Google Map in the presence of point id: