jquery each loop to add data() to each td column

ShanghaiD
ShanghaiD used Ask the Experts™
on
I have programatically created a table by constructing the HTML code and then appending it to the table body: $('#tsTable').find('tbody').append(strHTML.join(''));

I am now trying to loop through every td of every tr and add the text() of each td as a data() value for each of those tds.  (I will actually manipulate the text() first, but for this illustration I show simplifiied code for a straight copy of each text() to each data() item).
his is the code I am using (without success):
$.each($('#tsTable tbody tr'), function(row) {
 $.each([1,2,3,4,5,6,7,8], function(n,v){
 var theData = $(this)('td:nth-child(v)').text();
 $('#tsTable tbody tr td:nth-child(v)').data('val',theData);
 });
});

Open in new window

Any suggestions to get the data() from each td.text() would be appreciated!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
I think I have found an alternative "solution" in the jquery api (http://api.jquery.com/data/):
HTML5 data-* Attributes

As of jQuery 1.4.3 HTML 5 data- attributes will be automatically pulled in to jQuery's data object. The treatment of attributes with embedded dashes was changed in jQuery 1.6 to conform to the W3C HTML5 specification.
I can use this technique when I programatically create the strHTML.

However, I would still like to understand how I could make my above jquery code work correctly.
David S.Consultant & Challenge Subduer
Top Expert 2009

Commented:
Change
var theData = $(this)('td:nth-child(v)').text();

Open in new window

to
var theData = $('td:nth-child(v)', this).text();

Open in new window

OR
var theData = $(this).find('td:nth-child(v)').text();

Open in new window

Multitechnician
Top Expert 2014
Commented:
$('td', '#tsTable tbody').each(function(cell) {
          $(this).data('val', $(this).text());
});

Open in new window

Author

Commented:
This is the most elegant and simplest syntax for what I am trying to do.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial