Tono Nam
Tono Nam

Reputation: 36048

How can I set the variable equal to the innerHTML of an element

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

Answers (5)

Gajahlemu
Gajahlemu

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

Tono Nam
Tono Nam

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

jessegavin
jessegavin

Reputation: 75640

var tableHtml = document.getElementById("mytable").innerHTML;

Upvotes: 1

moe
moe

Reputation: 29704

alert(document.getElementById('id_of_the_element').innerHTML);

Upvotes: 0

David Tang
David Tang

Reputation: 93664

The same way you set it:

var tableHTML = document.getElementById('myTable').innerHTML;
alert(tableHTML);

Upvotes: 0

Related Questions