Reputation: 7067
<td id="hello"> OPD </td>
On my html page, this is how I am getting a td tag in a tr.
I want to get the value "OPD" in java script variable.
var td1 = document.getElementById("hello");
Can I get this value by performing any operation on td1.
Is there any other way to do this. Please help
Upvotes: 3
Views: 10013
Reputation: 60717
There are several ways to do this:
Either you use a DOM-Shim and just the following:
var td1 = document.getElementById('hello').textContent
Or you don't want to use a shim, then you have to use the following (thank IE):
var hello = document.getElementById('hello'),
td1
if ('textContent' in hello) {
td1 = hello.textContent // Standard way
}
else {
td1 = hello.innerText // IE way
}
Or you use jQuery:
var td1 = $('#hello').text()
However, don't use innerHTML. This is just bad for many reasons.
Upvotes: 1
Reputation: 3333
var td1 = (document.getElementById("hello")).innerHTML;
or
var td1 = ((document.getElementById("hello")).innerHTML).trim();
Upvotes: 2
Reputation: 46467
If you're interested in a jQuery answer...
var tdText = $('#hello').text();
or
var tdTextTrimmed = $.trim($('#hello').text());
Upvotes: 0