Reputation: 36048
I have a table and I would like to set a variable equal to the innerHTML of a table. I have seen that I can change the innerHTML of an element but I haven't been able to figure out how to see the contents of the element.
Upvotes: 0
Views: 12600
Reputation: 1263
You can try this also: Html code:
Table:<br/>
<table id="tbl" border="1">
<tr>
<td>Cell 1.1</td>
<td>Cell 1.2</td>
<td>Cell 1.3</td>
</tr>
<tr>
<td>Cell 2.1</td>
<td>Cell 2.2</td>
<td>Cell 2.3</td>
</tr>
</table>
<br />
Message:<br/>
<textarea id="message"/>
Javascript code:
var tbl = document.getElementById('tbl');
var tblinner = tbl.innerHTML; //save the innerHTML to a variable
var message = document.getElementById('message');
message.value = tblinner; //display the innerHTML value
See it in action from jsfiddle.net
Upvotes: 1
Reputation: 36048
I thought that innerHTML was a set only property and not read. I was having an error about 5 lines above the code that's why I was not being able to see the alert(). sorry about that!
Upvotes: 0
Reputation: 93664
The same way you set it:
var tableHTML = document.getElementById('myTable').innerHTML;
alert(tableHTML);
Upvotes: 0