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'
.text()
will return a string representation of all the text nodes in that element while .html()
will give you the presentation of all nodes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With