0

I'm creating a list dynamically and I need to be able to retrieve values from the table cells. The structure is:

<ul id="tester">
<li><div><table><tr><td></td><td id="samecellid">I WANT THIS TD VALUE..
<li><div><table><tr><td></td><td id="samecellid">I WANT THIS TD VALUE..
<li><div><table><tr><td></td><td id="samecellid">I WANT THIS TD VALUE..

Each list item has a table within it - this table only has one row, so the td id is unique within each list item but not within the list as a whole obviously.

The problem I have is I can't seem to get the value of the td cell. I have tried several ways, this is my latest and it doesn't work:

if (lengthoflist > 0) {    
    for (i=1; i<=lengthoflist; i++){    

    var ul = document.getElementById("tester");
    var mya = ul.getElementsByTagName("li")[i];                     
    var myb = mya.getElementsByTagName("div");      
    var myc = myb.getElementsByTagName("table");
    var myd = myc.getElementsByTagName("tr");
    var mye = myd.getElementById("samecellid");

    var celldata = mye.innerHTML;
}
}
0

4 Answers 4

1

getElementsByTagName returns an array so it's better like that:

if (lengthoflist > 0) {  
    var ul = document.getElementById("tester");

    for (i=1; i<=lengthoflist; i++) {    

        var mya = ul.getElementsByTagName("li")[i];                     
        var myb = mya.getElementsByTagName("div")[0];      
        var myc = myb.getElementsByTagName("table")[0];
        var myd = myc.getElementsByTagName("tr")[0];
        var mye = myd.getElementsByTagName("td")[1]; // the second

        var celldata = mye.innerHTML;
    }
}

But of course with a unique ID on td tags you can get it straight.

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

2 Comments

Thanks for that, I have moved my ul up. Unfortuantely I can't have unique ids for the td elements as the list is dynamically created and I would have no way of knowing what the ids were when I came back to refer to them.
this is not referencing the ids, but the index in the collection. It says pick td number 2 -> <tr> <td></td> <td id="samecellid">I WANT THIS TD VALUE..
0

Retrieve the li tags in your loop, then get the last td in each li. I modified your posted code a bit. You started your loop at 1, which means that you wouldn't retrieve the first list item. Arrays are 0 indexed, so start at 0. In length of list you used <= which would include a nonexistent element at the end of the list.

var liTags = document.getElementById("tester").getElementsByTagName('li'); 

for (var i = 0; i < liTags.length; i++) {
    var tdTags = liTags[i].getElementsByTagName('td');
    var celldata = tdTags[tdTags.length - 1].innerHTML; //last td
    console.log(celldata);
}

Update:

Here is a slightly more efficient method.

var liTags = document.getElementById("tester").getElementsByTagName('li'); 

for (var i = 0; i < liTags.length; i++) {
    var celldata = liTags[i].getElementsByTagName('tr')[0].lastChild.innerHTML;  //last td
    console.log(celldata);
}

3 Comments

Thanks for this. The For Loop is actually looping through the list so I'll try with moving your var liTags up to before the For statement.
@TW - I see. I updated my answer to reflect how I would do this.
Thats just what i'd ended up with. This is great thanks and solved this problem. Thank you!
0

If you used unique IDs you can get them directly without going down the HTML tree:

var mye = myd.getElementById("samecellid");
var myeA = myd.getElementById("samecellidA");

Comments

0

Do something like this.

<ul id="tester">
<li><div><table><tr><td></td><td id="samecellid1">I WANT THIS TD VALUE..
<li><div><table><tr><td></td><td id="samecellid2">I WANT THIS TD VALUE..
<li><div><table><tr><td></td><td id="samecellid3">I WANT THIS TD VALUE..

if (lengthoflist > 0) {    
    for (i=1; i<=lengthoflist; i++){
      var mye = document.getElementById("samecellid" + i);

      var celldata = mye.innerHTML;
  }
}

1 Comment

This would eb a good solution but I can't have unique ids for the td elements as the list is dynamically created and I would have no way of knowing what the ids were when I came back to refer to them.

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.