javascript - How do I reference a table row using jQuery - Stack Overflow

Afternoon,I wish to pass the row number of a table to a function to reference that specific row in that

Afternoon,

I wish to pass the row number of a table to a function to reference that specific row in that specific table, for example say I have this:

<table id="foo">
  <tr><td>some stuff...</td><tr>
  <tr><td>stuff</td><tr>
  <tr><td>more stuff</td><tr>
  <tr><td>some stuff</td><tr>
  <tr><td>some stuff</td><tr>
</table>

and I have looped through the table rows and obtained the index, so in this example say I wanted to do something with the third row (which would have an index of 2, the one which has the contents "more stuff"). And I passed this through a function, like this

manipulateRow(2)

and this is my whole function

function manipulateRow(rowIndex){

/* do something */

}

How do you refer the rowIndex parameter to the table within the function? For example:

$('#foo').child("tr")[rowIndex].html('<td>i now contain even more stuff!</td>'); // I know this is wrong, how do I make it right?

Sorry if I'm being a bit thick or not explaining myself.

Afternoon,

I wish to pass the row number of a table to a function to reference that specific row in that specific table, for example say I have this:

<table id="foo">
  <tr><td>some stuff...</td><tr>
  <tr><td>stuff</td><tr>
  <tr><td>more stuff</td><tr>
  <tr><td>some stuff</td><tr>
  <tr><td>some stuff</td><tr>
</table>

and I have looped through the table rows and obtained the index, so in this example say I wanted to do something with the third row (which would have an index of 2, the one which has the contents "more stuff"). And I passed this through a function, like this

manipulateRow(2)

and this is my whole function

function manipulateRow(rowIndex){

/* do something */

}

How do you refer the rowIndex parameter to the table within the function? For example:

$('#foo').child("tr")[rowIndex].html('<td>i now contain even more stuff!</td>'); // I know this is wrong, how do I make it right?

Sorry if I'm being a bit thick or not explaining myself.

Share Improve this question asked Jul 27, 2011 at 14:33 Mike SavMike Sav 15.3k31 gold badges102 silver badges150 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 2

Easy.

$("#foo tr:eq("+rowIndex+")").html("<td>i now contain even more stuff!</td>");

Learn more about jQuery selectors here: http://api.jquery./category/selectors/

that could work actually only like this:

$($('#foo tr')[rowIndex]).html('<td>i now contain even more stuff!</td>');

but best to use :eq

Try this

$('#foo > tr').eq(rowIndex).html('<td>i now contain even more stuff!</td>');
function manipulateRow(rowIndex) {
    var $row = $('#foo tr:nth-child(' + rowIndex + ')');
    ...
}

See nth-child-selector.

here you go:

<table>
    <tbody>
        <tr><td>Foo</td></tr>
    </tbody>
    <tfoot>
        <tr><td>footer information</td></tr>
    </tfoot>
</table>

$("#myTable > tbody").append("<tr><td>row content</td></tr>");

heres another example inline editing:

function editRow(row) {
    $('td',row).each(function() {
         $(this).html('<input type="text" value="' + $(this).html() + '" />');
    });
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745348272a4623667.html

相关推荐

  • javascript - How do I reference a table row using jQuery - Stack Overflow

    Afternoon,I wish to pass the row number of a table to a function to reference that specific row in that

    8小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信