user2232681
user2232681

Reputation: 833

text vs innerHTML in JQuery

I get the same result from both methods, but not sure why. Research on SO tells me this:

.text() returns JUST the text of that element and all of its descendant elements, where as .innerHTML returns all of the HTML in that element.

however, further research tells me this: The real issue is that text() and innerHTML operate on completely different objects.

Can I get some clarification?

HTML

<table id="table2">
<th> Col1 </th>
<th> Col2 </th>
<tbody>
<tr>
<td id="data">456</td>
</tr>
</tbody>
</table>

JQuery

$('td').click(function() {
    var x=$(this).text();
    alert(x); //returns '456'
})

var abc = document.getElementById('data');
var xyz = abc.innerHTML;
    alert(xyz); //also returns '456'

Upvotes: 6

Views: 8214

Answers (1)

Bilnaad
Bilnaad

Reputation: 63

.text() will return a string representation of all the text nodes in that element while .html() will give you the presentation of all nodes.

Upvotes: 6

Related Questions