Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript, getting value of a td with id name

Tags:

javascript

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?

like image 786
Strawberry Avatar asked Feb 22 '10 10:02

Strawberry


People also ask

How do you find the value of TD?

jQuery: code to get TD text value on button click. So our code to get table td text value looks like as written below. $(document). ready(function(){ // code to read selected table row cell data (values).

Can TD element have a value?

td elements don't have value. What you are looking for is to get the value of the attribute value of that element.

How can get TD value using ID in jQuery?

$(document). ready(function(){ var r=$("#testing":row2). val(); alert(r); });


1 Answers

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(); 
like image 185
rahul Avatar answered Sep 29 '22 15:09

rahul