2

I have HTML tables with data. There are some columns which are hidden, based on report generation. I am generating an Excel file from these table. For generating the Excel file, I have to provide the innerHTML data to the Excel function. The issue is that, innerHTML data contains hidden tags as well.

This results in Excel file showing the hidden column data. Is there a way I can remove the hidden tags' data?

14
  • Can you just remove all child elements and then get the innerHTML? Commented Jan 16, 2015 at 7:19
  • Posting a sample code will help. Essentially, in plain JS, you can iterate over DOM elements and exclude the element type='hidden' elements. Commented Jan 16, 2015 at 7:21
  • the solution is pretty straightforward, have you tried solving it on your own? if yes please post what you have tried in your question. Commented Jan 16, 2015 at 7:23
  • You should manipulate the DOM elements first, then get the innerHTML. Attempting to manipulate HTML as text is not pleasant. Do you have an example of how the columns are hidden? Commented Jan 16, 2015 at 7:29
  • Did the underlying solution work for you? Commented Jan 16, 2015 at 7:36

1 Answer 1

2

Using JQuery, you can do something like this:

var htmlString = '<<your innerHTML string>>';    
var obj = jQuery(htmlString);
var jQ = jQuery("<p>").append(obj);
jQ.children(":hidden").html("");
var newHtml = jQ.html();

Now use the newHtml in your code.

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

10 Comments

this might ruin the report layout. better clear the hidden element's inner html jQ.children(":hidden").html("")
@Banana agreed!! Updated my answer
This might clear the content of a hidden cell, but the cell will still be in the table, won't it? It's also a bit hit and miss, as cells with visibility:hidden or opacity:0 will not be cleared as they aren't selected by :hidden.
yes it will. as per op's requirements he needs to "remove the hidden tags' data"
Note: "The issue is that, innerHTML data contains hidden tags as well" which indicates to me that the entire element needs to be removed, otherwise there will (most likely) be a column of empty cells in the spread sheet.
|

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.