46

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?

Strawberry
  • 66,024
  • 56
  • 149
  • 197

8 Answers8

119

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
  • 184,426
  • 49
  • 232
  • 263
  • 1
    Interesting. Under which conditions is `textContent` the better choice? – Pekka Feb 22 '10 at 10:18
  • 1
    innerText isn't supported in FF. – rahul Feb 22 '10 at 10:31
  • 3
    @Pekka웃 `innerText` returns a version of the text similar to [what would show](http://perfectionkills.com/the-poor-misunderstood-innerText/) on screen (including new lines and spacing). `textContent` gets exactly what is between the start and end tag (including tabs and spaces) with all child tags removed (though their text is also included). In general, you should always use `textContent` to prevent compatibility issues with FF. – Trisped Mar 03 '16 at 01:22
10

use the

innerHTML 

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

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
9

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
  • 11,070
  • 4
  • 41
  • 61
3

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

document.getElementById('td-id').innerHTML
Qwerty
  • 1,732
  • 1
  • 13
  • 18
3

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
  • 377,238
  • 77
  • 533
  • 578
1

.innerText doesnt work in Firefox.

.innerHTML works in both the browsers.

Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107
Vipin
  • 67
  • 4
1

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

Select0r
  • 12,234
  • 11
  • 45
  • 68
0

if i click table name its shown all fields about table. i did table field to show. but i need to know click function.

My Code:

$sql = "SHOW tables from database_name where tables_in_databasename not like '%tablename' and tables_in_databasename not like '%tablename%'";


$result=mysqli_query($cons,$sql);

$count = 0;

$array = array();

while ($row = mysqli_fetch_assoc($result)) {

    $count++;

    $tbody_txt .= '<tr>';

    foreach ($row as $key => $value) {
        if($count  == '1') {
            $thead_txt .='<td>'.$key.'</td>';
        }
        $tbody_txt .='<td>'.$value.'</td>';


        $array[$key][] = $value;
    }
}?>
Ram Koti
  • 2,203
  • 7
  • 26
  • 36
Naveen Y
  • 1
  • 2