Skip to content Skip to sidebar Skip to footer

How To Obtain Only Visible Elements Inside An HTML Table Using JQuery?

I have the following problem: I have a table in which each row has some cells that are visible, and some that aren't. Something like this:

Solution 1:

To get all visible elements, you can use the :visible selector with this syntax :

$('td:visible')

But this won't enable you to get the html for the all table as if it didn't contain the hidden elements.

For that, you could temporarily duplicate the table and remove the not visible cells :

var t = $('table').clone();
t.appendTo(document.body);
t.find('td').not(':visible').remove();
var html = t.html();
t.remove();

Demonstration (open the console)


Solution 2:

This is late I know, but since I like learning ... ;-)

At jQuery :visible Selector, there's a nice example.

HTML:

<table>
...
</table>

<div id="output"></div>

JS:

var t = $('table').clone();
$('td', t).filter(function() { return $(this).css('display') == 'none'; }).remove();
var html = t.html();
$('#output').text(html); // This shows the HTML code

See JSFiddle


Post a Comment for "How To Obtain Only Visible Elements Inside An HTML Table Using JQuery?"