Convert DOMElement array to jQuery object

I am using the DataTables plugin for jQuery and should get one of the rows of the table. DataTables has a fnGetNodes function that returns an array with all the DOMElements of the table. I would like to use the jQuery selector to search for this string (I know the string identifier), but I need to convert the array to a jQuery object, is this possible?

+4
source share
3 answers

Logically you can do this with

var $row = $(dom_array).filter( function(index){ return $(this).attr('id') == 'some_id'; } ); 

this will return the jQuery object of the string with the specified id.

+3
source

To get a jQuery object from an array of nodes, you can simply pass it to jQuery:

 var nodes = [document.documentElement, document.documentElement.firstChild]; var extendedNodes = $(nodes); 
+5
source

According to http://api.jquery.com/jQuery/ you can do just that:

 jQuery( elementArray ) elementArrayAn array containing a set of DOM elements to wrap in a jQuery object. 

If this does not work, maybe your array is not the actual array, so you can try:

 $('#id',$($.makeArray(array))); 
+3
source

Source: https://habr.com/ru/post/1311963/


All Articles