1

I have a aspx file like has HTML like below:

enter image description here

I want to get the value 37.23961( as seen above).

how do i get this value ( td with iindex 6 of table 'gridview2) using javascript ?

MyHTML:

<table rules="all" id="GridView2" style="width:542px;border-collapse:collapse;" border="1" cellspacing="0">
    <tbody>
    <tr>
        <th scope="col">hname</th>
        <th scope="col">haddress</th>
        <th scope="col">hphone</th>
        <th scope="col">hhours</th>
        <th scope="col">hrating</th>
        <th scope="col">hlat</th>
        <th scope="col">hlong</th>
    </tr>
    <tr>
        <td>Kaiser Permanante</td>
        <td>200 Fremont boulevard</td>
        <td>5105199000</td>
        <td>Mon-Fri : 8:00 AM - 11:00 PM, Sat, Sun: 10:00 AM- 6:00PM</td>
        <td>3</td>
        <td>37.23961</td>
        <td>-121.800278</td>
    </tr>
</tbody>
</table>
6
  • Can you please copy-paste your HTML so we can manipulate it? Commented Dec 2, 2014 at 6:30
  • Is the value always in the 6th column of the second row in the table? Commented Dec 2, 2014 at 6:31
  • alert(document.getElementById("GridView2").getElementsByTagName("td")[5].innerHTML) try this Commented Dec 2, 2014 at 6:36
  • Check out my answer and my fiddle jsfiddle.net/2xww81m8 Commented Dec 2, 2014 at 6:43
  • Please find the answers below and close this question by marking the answer as acepted whichever satisfied your needs. Commented Dec 2, 2014 at 6:47

10 Answers 10

4

First access the appropriate tr and in that, access the appropriate td. Checkout the snippet below:

var tr = document.getElementsByTagName("tr")[1];

var td = tr.getElementsByTagName("td")[5];

var td_text = td.innerHTML;  // here is "37.23961"

document.getElementById("fetched").innerHTML = td_text;
#fetched{
  font-weight: bold;
}
<table rules="all" id="GridView2" style="width:542px;border-collapse:collapse;" border="1" cellspacing="0">
  <tbody>
    <tr>
      <th scope="col">hname</th>
      <th scope="col">haddress</th>
      <th scope="col">hphone</th>
      <th scope="col">hhours</th>
      <th scope="col">hrating</th>
      <th scope="col">hlat</th>
      <th scope="col">hlong</th>
    </tr>
    <tr>
      <td>Kaiser Permanante</td>
      <td>200 Fremont boulevard</td>
      <td>5105199000</td>
      <td>Mon-Fri : 8:00 AM - 11:00 PM, Sat, Sun: 10:00 AM- 6:00PM</td>
      <td>3</td>
      <td>37.23961</td>
      <td>-121.800278</td>
    </tr>
  </tbody>
</table>

<label>Fetched TD content: </label><span id="fetched"></span>

Sign up to request clarification or add additional context in comments.

3 Comments

I am using script tags in aspx file. Do i have to use something like <%- <%= <% to make the script run or something? :-/
@MonicaThaneer I never worked with ASPX but you may take a look at the official documentation about it: Using JavaScript Along with ASP.NET
@MonicaThaneer, put your script inside "script" tag. that will be enough.
1

You can get the value using

var gridTable = document.getElementById("GridView2");
if(gridTable && gridTable.rows[1].cells[5]){
      var result=gridTable.rows[1].cells[5].innerText;
}

If you want the actual float value and not string, then you can parse it.

parseFloat(result);

If you want to get all the row's 6th column value, You can refer this codepen: http://codepen.io/bhuvana/pen/OPyjWw

Comments

1

function myData(){
x = document.getElementById("GridView2").rows.length;

for(i=1;i<x;i++){
	var tr = document.getElementsByTagName("tr")[i];
	var tdl = tr.getElementsByTagName("td").length;
	
	for(j=0;j<tdl;j++){
		var td = tr.getElementsByTagName("td")[j];
     alert(td.innerHTML);
	}
}
}
<table rules="all" id="GridView2" style="width:542px;border-collapse:collapse;" border="1" cellspacing="0">
    <tbody>
    <tr>
        <th scope="col">hname</th>
        <th scope="col">haddress</th>
        <th scope="col">hphone</th>
        <th scope="col">hhours</th>
        <th scope="col">hrating</th>
        <th scope="col">hlat</th>
        <th scope="col">hlong</th>
    </tr>
    <tr>
        <td>Kaiser Permanante</td>
        <td>200 Fremont boulevard</td>
        <td>5105199000</td>
        <td>Mon-Fri : 8:00 AM - 11:00 PM, Sat, Sun: 10:00 AM- 6:00PM</td>
        <td>3</td>
        <td>37.23961</td>
        <td>-121.800278</td>
    </tr>
</tbody>
</table>
<button onclick="myData()">click</button>

Comments

1

check with this code using jquery.if possible provide jsfiddle.

$('#GridView2').find('td:eq(6)').html();

jsfiddle

2 Comments

This is not a jQuery question.
I have suggested to use jquery . which makes code easy in one step.
0

Use this:

document.getElementsByTagName("td")[6]

Basically, you're getting all the elements with the tag name "td", and the brackets on the end specify which element to choose.

Hope this helps ;)

Comments

0

You need to get the HTML content of the 6th element in the markup which can be done in the following way

document.getElementsByTagName("td")[6].innerHTML

Comments

0

If you want to use JavaScript, then document.querySelectorAll(css selector) is your option. Get all td element that is the sixth child of its parent:

var tds = document.querySelectorAll("#GridView2 td:nth-child(6)");

To get its text, you may want to loop it.

for (var i = 0, len = tds.length; i < len; i++) {
  alert(tds[i].textContent || tds[i].innerText);
}

Comments

0

Do give ID to your TDs, if you are generating table, you can give index ids.

For example :
for(var i=0;i<rows;i++)
{
tableCells = tablesCells + "<td id=td" + i + ">"+data[i] + "</td>";
}

Then you can identify what index's value you need and use following methods to get values

var tdElem = document.getElementById ( "td" +id );
var tdText = tdElem.innerHTML;
OR


$("#td"+id).html();

2 Comments

Sry I have nil knowledge about jquery i am getting a synatx error at 'in' for line in JQuery
you can use javascript, syntax given above
0

It would be really easier if you would use jQuery. But as you have your specific index and the table structure is fixed you can have your job done simply using nested .getelementsbytagname()

javaScript :

alert(document.getElementById("GridView2")
    .getElementsByTagName("tbody")[0]
    .getElementsByTagName("tr")[1]
    .getElementsByTagName("td")[5]
    .innerHTML);

jQuery

If you do prefer jQuery you may have a look,

$("#GridView2 tbody tr:nth-child(2) td:nth-child(6)").html();

javaScript / jQuery

Comments

0

I have all the td's from the table in the same class="tds" and with that way i take all the data with the for loop.

   var elements = document.getElementsByClassName("tds");
        for(var i = 0; i < elements.length; i++){
            (function(index) {
                console.log(elements[index].lastChild.data);   
            });
        })(i);
    }

In your situation:

var sixth = elements[5].lastChild.data;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.