Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'
like image 510
user2232681 Avatar asked May 24 '13 21:05

user2232681


1 Answers

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

like image 87
Bilnaad Avatar answered Sep 24 '22 02:09

Bilnaad