Hi Folks,
I seem to have quite a conundrum here and I'm at my wits end. Essentially I have a HTML table where I'm trying to place a message at the top of the table should a value appear later in the table.
My code is basically as follows...
- ...
-
<table>
-
<script language="javascript">
-
var myval = getmyval();
-
if(myval == "True") {
-
document.write("<tr><td>My Val is True</td></tr>");
-
}
-
</script>
-
-
<tr><td id="my_val" name="my_val">True</td></tr>
-
<script language="javascript">
-
function getmyval() {
-
return document.getElementById('my_val').innerHTML;
-
}
-
</script>
-
</table>
-
-
...
The code errors out with "Object Expected" on the "var myval = germyval();" call.
This makes me think that the function getmyval() needs to appear before the call, however if I do that I run into the getElementById call failing since that value isn't present at declaration.
Can anyone help me solve this?
Ryan
The problem is as you have stated. You are just seeing a secondary manifestation of the same problem. When getmyval() is called it doesn't exist.
When getmyval() exists, the element itself doesn't exists.
Solution: create a hidden div on the page:
[HTML]
<div id="myvalStatus" style="display:none;"></div>
[/HTML]
Then, your javascript should run in a window.onload handler. This ensures that it won't try to execute until all the code
and page elements are in place.
Something like this (caution: untested):
-
<head>
-
<script type="text/javascript">
-
function setMyvalStatus() {
-
var myval = document.getElementById('my_val').innerHTML;
-
if(myval == "True") {
-
var myvalDiv = document.getElementById('myvalStatus');
-
myvalDiv.innerHTML = "It's True!";
-
myvalDiv.style.visibility = "";
-
myvalDiv.style.display = "";
-
}
-
}
-
</script>
-
</head>
-
<body onload="setMyvalStatus()">
-
-
... html goes here ...
-
-
</body>
-