Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
hi again. i was trying to sort this for about hours from my last post. with a refresh browser method, what did i miss here. this script doesn't want to write the supposed value in the assigned id.
[script]
var hpStatus = Math.floor(Math.random()*11);
if (hpStatus == 0) {
document.getElementById("myHP").innerHTML = "Low"
} else if (hpStatus < 4) {
document.getElementById("myHP").innerHTML = "Medium"
} else {
document.getElementById("myHP").innerHTML = "High"
}
[/script][html]
Status: [span id="myHP"] [/span]
[/html]

finally, here's after hours of test n try. here's what i found which seems like rules of javascript:
1) .innerHTML must be assign to some onclick function. if u expect it writes on refresh.. let me know if u can do it.
2) playing with math.random + getelementbyid + innerhtml must be careful on where u assign the variables(global/local). compares this two examples, and u see the difference.
Example1: Not Working
<script type="text/javascript">
var randomNum = Math.floor(Math.random()*11);
function varGlobal() {
if (randomNum==0) {
document.getElementById("myNum").innerHTML = randomNum
document.getElementById("myTxt").innerHTML = "low"
} else if (randomNum<3) {
document.getElementById("myNum").innerHTML = randomNum
document.getElementById("myTxt").innerHTML = "medium"
} else {
document.getElementById("myNum").innerHTML = randomNum
document.getElementById("myTxt").innerHTML = "high"
}
}
</script>
[input type="button" value="search" onclick="varGlobal()"]
[div id="myNum"][/div]
[div id="myTxt"][/div]Example2: Working
<script type="text/javascript">
function varLocal() {
var randomNum = Math.floor(Math.random()*11);
if (randomNum==0) {
document.getElementById("myNum").innerHTML = randomNum
document.getElementById("myTxt").innerHTML = "low"
} else if (randomNum<3) {
document.getElementById("myNum").innerHTML = randomNum
document.getElementById("myTxt").innerHTML = "medium"
} else {
document.getElementById("myNum").innerHTML = randomNum
document.getElementById("myTxt").innerHTML = "high"
}
}
</script>
[input type="button" value="search" onclick="varLocal()"]
[div id="myNum"][/div]
[div id="myTxt"][/div]yet i have no idea for technical explanation on this. but my problem solved.

I didn't read through most of your 2nd post, but looking at your first post, the problem is pretty obvious. Javascript is run in-line as the page loads. There is nothing special about innerHTML that requires it to run via an onclick event.
In your example above youhave code that is trying to reference the element with the id "myHP", but since the javascript is running at the top of the page as it loads, that element does not yet exist.
I would suggest either moving the javascript after that field or put the javascript within a function and run it via the onload event. Yet another option is have the code run durin the page load, but instead of trying to set the value of innerHTLM just save the value to a variable. Then where you want the text to show, use a document.write
Michael J

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |