0

I'm using javascript to covert an CSV file to HTML table, but I need to give any table column a diferent class, so I can use only the columns that I need.

I have no ideia how to use a dynamic class (instead of td.class) name for every colums the script create, anyone here knows any solution for that?

My code:

$.each(items, function(itemCount, item) {

tableHTML += '<td class="' + td.class + '">' + item + '</td>';

});

tableHTML += '</tr>';

Thanks!

4
  • class is a reserved word, you should use className or just anything other than a reserved word. Your question is a bit confusing, are you trying to assign a class to each column? Commented Feb 12, 2016 at 20:14
  • what should "dynamic class name" reflect in your logic? Commented Feb 12, 2016 at 20:14
  • I wrote td.class just in my example, and yes, I need every column to have a diferent name. With Dynamic class name I mean a different class name for every column. Commented Feb 12, 2016 at 20:17
  • It it better to create element by using JQuery like $tds.append( $('<td>').addClass(yourClassName).html(item)); Then out of the loop YourTableRowElement.append($tds) Commented Feb 12, 2016 at 20:17

1 Answer 1

1

One way is to use modulo to append the column number:

var columnCount = 4;

$.each(items, function(itemCount, item) {
  var currentColumn = (itemCount % columnCount) + 1// add 1 because of zero index
  tableHTML += '<td class="column-' + currentColumn + '">' + item + '</td>';
});

I’m guessing you could probably get the columnCount dynamically, but would need to see the rest of your code to show that.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.