i have two id's that are the same from two different table.

var newtext = document.getElementById(number).innerHTML;

this code is grabbing the wrong id. the id is a counter/variable in a loop so it changes, so i can't do getElementByName.. how do i get around this?

i can't do getElementByName

Don't confuse getElementsByName with getElementByName.

two id's that are the same

At risk of pointing out the obvious, making the id's different would solve the problem. :) You could have one loop generate 'A'+### and the other generate 'B'+###. Of course that is a complete fix only if you have some way of knowing the ### part.

If something like that isn't possible, what you do next depends on what you know about the tables.

get around this?

This

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
  <head>
    <meta name="generator" content=
    "HTML Tidy for Windows (vers 25 March 2009), see www.w3.org">
    <title></title>
  </head>
  <body>
    <table id='tm'>
      <tr>
        <td>
          test
        </td>
        <td>
          test
        </td>
        <td id='2'>
          XXX
        </td>
      </tr>
    </table>
    <table id='tn'>
      <tr>
        <td>
          test
        </td>
        <td>
          test
        </td>
        <td id='2'>
          YYY
        </td>
      </tr>
    </table>
  <script>
var number=2;
var cTDs = document.getElementsByTagName('td')
for (var j = cTDs.length, i = 0; i < j; i++) {
    if (cTDs[i].getAttribute('id') == number && cTDs[i].parentNode.parentNode.parentNode.getAttribute('id') == 'tn') {
        alert(cTDs[i].innerHTML)
    }
}
  </script>

  </body>
</html>

shows a brute force solution that depends on two things:
a unique attribute on each <table> element (in this example, id)
knowledge of the structure of both tables (in this example, they are identical)

As you can see, the fact that the object you want is in a table makes the code fairly messy even in this toy example. In the general case (there is no easy way to tell the tables apart, they may appear in any order on the page, and/or they are dynamic), a solution [although still possible] is more complicated.

Although ids should always be unique (I believe as stated by the w3c), getElementById does NOT have to be specifically used with just "document". It can be used with any element that has childNodes, as can getElementsByTagName and getElementsByName.

Also in most browsers, getElementsByName only works with some elements. Like I don't think you can use it to get images or divs. I remember seeing this somewhere.

Should have done some research... I use getElementsByTagName with other elements, so I was just assuming getElementById would work the same. But that just further proves the point that element ids need to be unique.

In XML the id's are forced to be unique. Other than that, it is only assumed that they will be.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.