0

I have a table that is meant to be a specific size, the value in the 5th cell of the table is always truncated and I want to add a title attribute that is the full value of the 5th cell.

I have been looking at some solutions such as:

jQuery and Table, grab every cell in the NTH column

Adding attribute in jQuery

But those aren't exactly what I want. I've tried this:

$('#tableID > tbody > tr:nth-child(n) > td:nth-child(5)').attr('title', 'hello');

And this does give every 5th cell and add the title attribute to it. But the problem is that I want to be able to get an array or list of the 5th cells and be able to make the .text() of the cell into the title attribute. I tried to put $('#tableID > tbody > tr:nth-child(n) > td:nth-child(5)') into a variable, but that doesn't give me the array.

Is there a way of getting the full list of 5th column of every row in my table?

Robert Harvey
  • 178,213
  • 47
  • 333
  • 501
user3369494
  • 123
  • 11

1 Answers1

2

You're almost there. Just loop over the collection with each:

$('#tableID > tbody > tr:nth-child(n) > td:nth-child(5)').each(function() {
  $(this).attr('title', $(this).text());
});

This code, in each iteration, takes the text of the cell and assigns it into the title attribute.

31piy
  • 23,323
  • 6
  • 47
  • 67
  • Thank you! I didn't know you could just use the .each() function on it. I tried to do a for(;;) loop through it and i wasn't getting what I wanted so I assumed that didn't work. – user3369494 Apr 18 '18 at 15:58