-2

This should also work on IE8

My tr rows element has multiple td elements which each td contains multiple divs. Some divs has the class "cell-data"

In all these divs on one row. I am trying to find and manipulate the second and third div with the class "cell-data"

I can get the first child correctly but not the third one. I searched here on SO also tried eq() method but still cant find.

$( ".rows" ).each(function() {
    var height = $(this).find('.cell-data:nth-child(1)').css("height");
    var height3= $(this).find('.cell-data:nth-child(3)').css("height") ;
});

And Html looks like this

                  <tr class="rows">
                                    <td class="cell-td">  
                                        <div class="row">
                                            <div class="col-3">
                                                <div>
                                                </div>
                                            </div>
                                            <div class="col-3">
                                                <div  class="cell-data">

                                                </div>
                                            </div>
                                        </div>
                                    </td>
                                    <td class="cell-td">

                                            <div class="cell-data" >

                                                </span>
                                            </div>


                                    </td>
                                    <td class="cell-td">
                                        <div class="cell-data">

                                        </div>
                                    </td>
                                </tr>
Spring
  • 11,333
  • 29
  • 116
  • 185

2 Answers2

1

.cell-data:nth-child(3) does not give you the third div with the class. It gives you the third child of the parent instead.

Use .cell-data:nth-of-type(3) instead.

simonzack
  • 19,729
  • 13
  • 73
  • 118
-2

As @Vld suggested problem was due to the td elements, so I fixed by;

$(".rows").each(function () {
        var td1 = $(this).find('.cell-td:nth-of-type(1)');
        var td2 = $(this).find('.cell-td:nth-of-type(2)');
        var td3 = $(this).find('.cell-td:nth-of-type(3)');

        var height = $(td1).find('.cell-data:nth-of-type(1)').css("height");
        $(td2).find('.cell-data:nth-of-type(1)').css("height", height);
        $(td3).find('.cell-data:nth-of-type(1)').css("height", height);
    });
Spring
  • 11,333
  • 29
  • 116
  • 185