Click here to Skip to main content
15,891,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the following:

<asp:panel id="myASPPanel" runat="server"... />


There are some dynamically created tables in the codebehind. Let's say there are 3 tables and we only want to get 1 of them, the first one. We are trying to do this using Javascript.

We have a function that RECEIVES the 'myDiv' ID.
We are then able to get the number of tables and iterate through them. However, when we get Table1, we only get the CONTENTS INSIDE the table, not the tags/html/attributes, etc.

Let's say this is the table:

MyTD


We are only able to get "MyTD", NOT the entire table. We want to store the entire table, along with its tags (just as it has been typed above) and display it.

We noticed that myDiv.getElementsByTagName('table').length works (for determining the number of tables inside of the DIV), but when we want to store the entire table in a variable, we do this: myDiv.getElementsByTagName('table')[i].innerHTML (this only gives us the 'MyTD' text, not tags, not HTML, not entire table element and its attributes) and if we use this: myDiv.getElementsByTagName('table')[i], what shows on the screen is [object HTMLTableElement]

How can we get the entire table along with its HTML, attributes, in-line style, etc?

Thank you.

What I have tried:

JavaScript
myDiv.getElementsByTagName('table')[i].innerHTML

JavaScript
myDiv.getElementsByTagName('table')[i]
Posted
Updated 15-Nov-16 3:50am

1 solution

The innerHTML property returns the HTML between the opening and closing tags of the specified element - the "inner" HTML.

To get the HTML including the opening and closing tags - the "outer" HTML - use the outerHTML[^] property.
JavaScript
myDiv.getElementsByTagName('table')[i].outerHTML
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900