Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

JS: getElById & innHTML prob

Original Message
Name: mie2com
Date: August 14, 2007 at 17:27:44 Pacific
Subject: JS: getElById & innHTML prob
OS: XP
CPU/Ram: 3
Model/Manufacturer: Dell
Comment:
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]


Report Offensive Message For Removal


Response Number 1
Name: mie2com
Date: August 14, 2007 at 21:42:03 Pacific
Subject: JS: getElById & innHTML prob
Reply: (edit)
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.


Report Offensive Follow Up For Removal

Response Number 2
Name: Michael J (by mjdamato)
Date: August 14, 2007 at 22:51:19 Pacific
Subject: JS: getElById & innHTML prob
Reply: (edit)
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


Report Offensive Follow Up For Removal




Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: JS: getElById & innHTML prob

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software




acer 312T BIOS problem

K7 Turbo possible max fsb?

Pc anywher problem

WinFLP & OE/Outlook2003

Computer resets after a few minutes


The information on Computing.Net is the opinions of its users. Such opinions may not be accurate and they are to be used at your own risk. Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE

All content ©1996-2007 Computing.Net, LLC