Mathias
Mathias

Reputation: 334

jQuery DataTables: How to get row index (or nNode) by row id of tr?

I have a dataTables <table id="myTable">. I would like to fnUpdate() and fnDestroy() my rows. every row has an id, eg: <tr id="16">. To fnUpdate()/fnDestroy() the appropriate <tr>, I need to get that row's index. For this I try to use fnGetPosition(), but the way I try it is not the way to do it:

$("#myTable").fnGetPosition( $("#16") )

results in

TypeError: nNode.nodeName is undefined [Break On This Error] var sNodeName = nNode.nodeName.toUpperCase();

Which makes sense, as fnGetPosition() expexts nNode (in my case a HTMLTableRowElement).

How do I get the HTMLTableRowElement that has id="16" ?

EDIT: A correct answer to my question is: document.getElementById("16"). Based on that, I would like to change my question to:

Why does

$("#myTable").fnGetPosition( document.getElementById("16") ) 

work, but

$("#myTable").fnGetPosition( $("#16") )

fails?

Upvotes: 7

Views: 25954

Answers (3)

Nicola Peluchetti
Nicola Peluchetti

Reputation: 76880

You sould do:

var oTable = $('#myTable').dataTable();
oTable.fnGetPosition( $("#myTable #16") );

Upvotes: 1

Michael Kopec
Michael Kopec

Reputation: 231

For anyone who still has this problem, try this:

$("#myTable").fnGetPosition( $("#16")[0] )

To get the same result as document.getElementById you should access the first element in the jQuery object.

Upvotes: 23

Ashkan Aryan
Ashkan Aryan

Reputation: 3534

document.getElementById() returns a DOM object, and everything on the DOM object will be inherently accessible.

JQuery's $('#...') returns a wrapper around a single DOM object OR a set of DOM objects (depending on the selector) and as such, it does not return the actual DOM Object. It makes it easier to work with DOM objects.

The reason you are getting that error in the second case would be that $(#...) is not actually a DOM object.

Upvotes: 12

Related Questions