0

I want to backup an html table to afterwards filter it using jquery:

$('.row').closest('td').not(':contains(' + v + ')').parent('tr').remove();

Since I do remove() I have to back up the rows before:

var allTable = $('#mytable').html();

And then, when filter is performed I turn back to previous table data:

$('#mytable').html($(allTable));

But this does not work. If I do:

alert($(allTable).filter('tr').length);

next to the first assignment, zero rows are returned.

Please, can you assist me?

1
  • 3
    Don't remove, just hide. Commented Nov 6, 2012 at 12:29

3 Answers 3

1

filter() is used to find elements within an array of elements. This isn't what you need. You're looking to find() the child elements within another. Also, storing the HTML only to turn it back in to a jQuery object is a little redundant - you may as well just store the jQuery object itself. Try this:

var $table = $('#mytable');
$table.remove(); // use your existing logic here
alert($table.find('tr').length);
$table.appendTo('body'); // add the table back in to the DOM when conditions are met

Example fiddle

Sign up to request clarification or add additional context in comments.

Comments

0

I ran into a similar issue when using a highlight function. I solved it by cloning the table into a hidden div and restoring it from there, instead of from a variable. see jquery highlight() breaking in dynamic table

Comments

0

Did you solve this problem?

I suggest a workaround.

Instead of using your cloned table, make a (temporary) copy of it and use it for alert.

var alertTable = allTable;
alert($(alertTable).filter('tr').length);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.