Elias Garcia Mariño Elias Garcia Mariño - 1 year ago 99
jQuery Question

jQuery parse HTML element and get it's inner HTML

I have a table in HTML with table rows, but some elements have an

elements inside, so i want to get the inner html of that span.

I have an array with all
elements (not jQuery Objects, because I get it from the DataTables plugin API), so I have this code:

if (data[i+1].includes("span")) {
var label = $.parseHTML(data[i+1]);
// editFormInputs.eq(i).val(label.first().html());
} else {

So the problem is on the commented line. I tried to convert the html to jQuery element with
, which returns an array. This works fine, but when I try to get the first element of the array with the
function it gives me an error
first() is not a function
, so what I am doing wrong? Thanks.

Answer Source

parseHTML is one of the rare jQuery functions that doesn't give you a jQuery object; it gives you an array:

jQuery.parseHTML( data [, context ] [, keepScripts ] )

Returns: Array

Description: Parses a string into an array of DOM nodes.

(my emphasis)



Note that label[0] will be the tr element. It wasn't clear to me that you wanted the HTML of the tr element vs. one of its spans, but in any case, that's the reason for the error.

If you want the HTML of (say) the first span in the tr:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download