tags:

views:

157

answers:

6

Originally I was using input with an id and grabbing it's value with getElementById. Currently, I'm playing with <td>s. How can I grab the values of the <td>?

Originally I used:

<input id="hello">
document.getElementById("hello").value;

Now I want to get value of <td>, but I don't know how;

<td id="test">Chicken</td>
<td>Cow</td>

Edit: What's the difference between textContent, innerHTML, and innerText? Edit2: This off topic, but I thought it was funny. If you read td real fast, it sounds like titty.

+3  A: 

use the

innerHTML 

property and access the td using getElementById() as always.

Pekka
+1  A: 

If by 'td value' you mean text inside of td, then:

document.getElementById('td-id').innerHTML
Qwerty
Is it supposed to have a hyphen?
Doug
@Doug No, it's just an example. You need to put your ID in there.
Pekka
A: 

Have you tried: getElementbyId('ID_OF_ID').innerHTML?

Select0r
+2  A: 

To get the text content

document.getElementById ( "tdid" ).innerText

or

document.getElementById ( "tdid" ).textContent

var tdElem = document.getElementById ( "tdid" );
var tdText = tdElem.innerText | tdElem.textContent;

If you can use jQuery then you can use

$("#tdid").text();

To get the HTML content

var tdElem = document.getElementById ( "tdid" );
var tdText = tdElem.innerHTML;

in jQuery

$("#tdid").html();
rahul
Interesting. Under which conditions is `textContent` the better choice?
Pekka
innerText isn't supported in FF.
rahul
A: 

Again with getElementById, but instead .value, use .innerText

<td id="test">Chicken</td>
document.getElementById('test').innerText; //the value of this will be 'Chicken'
anthares
A: 

For input you must have used value to grab its text but for td, you should use innerHTML to get its html/value. Example:

alert(document.getElementById("td_id_here").innerHTML);

To get its text though, use:

alert(document.getElementById("td_id_here").innerText);
Sarfraz