Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 566
Mathias Avatar asked Sep 21 '11 16:09

Mathias


3 Answers

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.

like image 165
Michael Kopec Avatar answered Nov 20 '22 14:11

Michael Kopec


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.

like image 28
Ashkan Aryan Avatar answered Nov 20 '22 13:11

Ashkan Aryan


You sould do:

var oTable = $('#myTable').dataTable();
oTable.fnGetPosition( $("#myTable #16") );
like image 1
Nicola Peluchetti Avatar answered Nov 20 '22 14:11

Nicola Peluchetti