JQuery: selecting table columns by index

I have a MyTable table and I want to select column i using a loop and test, if the column I have a specific css class.

I tried this:

thestop = 4; // simplified for clarity

for (i = 0; i < thestop; i++){
   if ( $('#MyTable .th').eq(i).hasClass('MyClass') )
      { $(this).width('60'); }
}

Of course, there are a few problems with this, and I cannot figure it out. Any ideas are welcome!

Thank.

+3
source share
2 answers
$('#MyTable th.MyClass').each(function(i) {
    $(this).width( arr[i] );
}); 

where arris an array containing the widths.

+4
source

What I immediately noticed is that it .thwill find elements with elements of the th class, and not <th>(use plain for this th). Also, I'm not sure what $(this)refers to the selected item in this case - I think it behaves the same as in functions .each(function() {})...

, , :

thestop = 4;
$('#MyTable th.MyClass:lt('+thestop+')').width('60px');

<th> thestop ( jQuery: lt selector), . - "th", .

, !

+1

Source: https://habr.com/ru/post/1793711/


All Articles