0

My file which is called 'table.htm' (trying to make a table) is below:

<!DOCTYPE html>
<html lang="en-AU">
<head>
<script type="text/javascript">
var thOne = document.getElementById("thOne");
alert(thOne)
//thOne.innerHTML = String(thOne.value);
</script>
</head>
<body>
<table>
<tbody>
<tr id="trOne">
<th value="1" id="thOne">
</th>
<td value="" id="tdOne">
</td>
</tr>
</tbody>
</table>
</body>
</html>

It shouldn't alert null, but it does. The commented out line should set id("thOne").innerHTML to it's value (1 in a string) but instead says it does not have a value (as TypeError). I know that, but how do I set the innerHTML to '1'?

W D
  • 204
  • 1
  • 10

1 Answers1

1

You can get the element after body loaded. e.g.

<!DOCTYPE html>
<html lang="en-AU">

<head>
  <script type="text/javascript">
  function bodyLoad() {
    var thOne = document.getElementById("thOne");
    var value = thOne.getAttribute('value');
    alert("value is " + value);
  }
  </script>
</head>

<body onload="bodyLoad()">
  <table>
    <tbody>
      <tr id="trOne">
        <th value="1" id="thOne">
        </th>
        <td value="" id="tdOne">
        </td>
      </tr>
    </tbody>
  </table>
</body>

</html>
Lon Yang
  • 696
  • 5
  • 12