7

I have a jQuery datatable row:

 table = $('#reports').DataTable()
 row = table.row(rowIndex)

How do I get the HTML class from it? (It is striped, and I want to find out if it is odd or even.)

I have tried:

row.hasClass('odd')

row.className

row.attr('class')

Any ideas?

3 Answers 3

7

Use node with className:

row.node().className;
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks :) DataTables documentation does leave something to be desired.
The solution used in the fiddle for my now deleted answer here -> jsfiddle.net/d9r5n0hp
@davidkonrad, your answer may actually be better. Try mine with row 4. I get nothing, while you get "odd."
Have undeleted my answer, but still thinks yours are better. Have not tested in depth. jsfiddle.net/d9r5n0hp/1
Hmm. You are right. It is in fact a very, very good question. Have to go to bed, but will tomorrow look into what the he** I have done myself, for this particular situation. I know I have been here before.
3

It is a really a good question. The ordinary jQuery way, by using row.index():

var rowClass = $("#example tbody tr:eq(" + row.index() + ")").attr('class');

Proof of concept -> http://jsfiddle.net/7jy46wz4/

Comments

0

Taking what @rick-hitchcock said, another approach is to use the following code in validations:

var hasOddClass = $(row.node).hasClass("odd");

which, by the examples you gave, should be closer to what you want.

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.