1

I'm a bit stuck trying to resolve this problem. I need get content of a few cells, and after this do update inner text of this cells

For example, I need to get values of cells with class = 2 and class = 4 in each row, and after this, update their inner text

I tried to do it by this code:

$(element).each(function ()
{
   // code code code
});

But isn't really something, what i need, because this code just get each element at each row, but i need get a few elements at each row at one time

It's should be lookin something like :

  1. get row.
  2. get elements with class = 1 and class = 2 at current row
  3. send their content to script
  4. update content of cells
  5. get next row
  6. etc

Here is example table:

<table>
    <tr>
        <td class="1"> Cell One </td>
        <td class="2"> Cell Two </td>
        <td class="3"> Cell Three </td>
        <td class="4"> Cell Four </td>
    </tr>
    <tr>
        <td class="1"> Also one Cell One </td>
        <td class="2"> Also one Cell Two </td>
        <td class="3"> Also one Cell Three </td>
        <td class="4"> Also one Cell Four </td>
    </tr>
</table>

**

And also. .

**

I need send Ajax Query, for each row in table, with inner text of 2 cells of current row ajax.php?id=column1&val=column2

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

2 Answers2

2

You can do something like

Also I think what you are trying is whether the current td has class 1 or 2 for that you can use .hasClass()

$('table').find('tr').each(function(){
    var $tr = $(this);
    $tr.find('.1, .2').html(function(idx, html){
        var $td = $(this);

        if($td.hasClass('1')){
            return html + ' class-1';
        } else if($td.hasClass('2')){
            return html + ' class-2';
        }

        return html + ' modified'
    })
})

Demo: Fiddle

Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
1

The following will give you a set of all class="1" and class="2" in one go:

$('tr .1, tr .2')

You may then do something like this:

$('tr .1, tr .2').each(function() {
    if($(this).is('.1')) {
       // class="1" stuff
    } else {
       // class="2" stuff
    }
});

If you prefer to do the iteration by row, you could just select the rows, $('tr'), and then find the children of interest inside the iteration:

$('tr').each(function() {
    var cols = $('.1, .2', this);
    // do something with cols
});

There are many ways forward; the best solution would depend on the details of what you're trying to acheive.

David Hedlund
  • 128,221
  • 31
  • 203
  • 222