I have the following table. What do i need to use in JavaScript to read the number from the below cell named "number"?
<table>
<tr>
<td id="number">56656.87</td>
</tr>
</table>
I have tried the following however it does not work.
document.getElementById('number');
I have the following table. What do i need to use in JavaScript to read the number from the below cell named "number"?
<table>
<tr>
<td id="number">56656.87</td>
</tr>
</table>
I have tried the following however it does not work.
document.getElementById('number');
Share
Improve this question
asked Mar 21, 2012 at 16:39
JackTheJackJackTheJack
1871 gold badge1 silver badge10 bronze badges
4 Answers
Reset to default 7Using raw JavaScript:
var text = document.getElementById('number').innerText;
Using jQuery:
var text = $('#number').text();
The innerHTML
property holds the contents of that cell. If you got [object Object] on your last attempt you're almost there. This is how you do it:
var n = document.getElementById('number').innerHTML;
Make sure there's no other number id on that page.
You could use jquery:
$(function(){
alert($("#number").html());
});
http://jsfiddle/fG5nF/
If you use jQuery, you can just do this:
var number = $("td#number").text();