Page 1 of 1

getElementById + parseInt problem

Posted: Mon Oct 04, 2010 7:40 pm
by internalgrenade
when i click submit the num value equals NaN. what should i do?
i don't think im parsing the number right.
any thoughts?

Code: Select all

<html>
<head> 
</head>
<body>
<input type="text" name="txt1" id="txt1" >
<input type="button" value="Submit" onclick="function1()"><br />
<input type="text" name="txt2" id="txt2">
<script type="text/javascript">
var randomNum = parseInt(100 * Math.random());
var num = parseInt(document.getElementById("txt1").innerHTML);
function function1() {
while (randomNum != num) {
if (randomNum < num) {
document.txt2.innerHTML.write("Your Value Is Too Big");
}
if (randomNum > num) {
document.txt2.innerHTML.write("Your Value Is Too Small");
}
if (num==randomNum) {
document.txt2.innerHTML.write("You Are Correct");
}
}
}
</script>
</body>
</html>

Re: getElementById + parseInt problem

Posted: Tue Oct 05, 2010 3:53 am
by VladSun
Use the value property instead of innerHTML

Re: getElementById + parseInt problem

Posted: Wed Oct 06, 2010 10:03 pm
by internalgrenade
i think that solved a problem but on line 12 i still have a infinite loop.
-Internal

Re: getElementById + parseInt problem

Posted: Thu Oct 07, 2010 12:22 am
by internalgrenade
after that i encountered a new error.
this is what it said. even though i already defined it as a variable.
document.txt2 is undefined

Code: Select all

<html>
<head> 
</head>
<body>
<input type="text" name="txt1" id="txt1" onchange="">
<input type="button" value="Submit" onclick="function1()"><br />
<input type="text" name="txt2" id="txt2">
<script type="text/javascript">
var randomNum = parseInt(100 * Math.random());
var num = parseInt(document.getElementById("txt1").value);
var txt2 = document.getElementById("txt2").innerHTML
function function1() {
while (randomNum != num) {
if (randomNum < num) {
document.txt2.innerHTML.write("Your Value Is Too Big");
}
if (randomNum > num) {
document.txt2.innerHTML.write("Your Value Is Too Small");
}
if (num==randomNum) {
document.txt2.innerHTML.write("You Are Correct");
}
}
}
</script>
</body>
</html>

Re: getElementById + parseInt problem

Posted: Thu Oct 07, 2010 4:33 am
by VladSun
VladSun wrote:Use the value property instead of innerHTML
Again ...

Re: getElementById + parseInt problem

Posted: Sun Oct 10, 2010 5:05 am
by internalgrenade
i still receive a error. after i changed the script.

<html>
<head>
</head>
<body>
<input type="text" name="txt1" id="txt1" onchange="">
<input type="button" value="Submit" onclick="function1()"><br />
<input type="text" name="txt2" id="txt2">
<script type="text/javascript">
var randomNum = parseInt(100 * Math.random());
var num = parseInt(document.getElementById("txt1").value);
var txt2 = document.getElementById("txt2").innerHTML
function function1() {
while (randomNum.value != num.value) {
if (num.value < randomNum.value) {
document.txt2.innerHTML.write("Your Value Is Too Big");
}
if (randomNum > num) {
document.txt2.innerHTML.write("Your Value Is Too Small");
}
if (num==randomNum) {
document.txt2.innerHTML.write("You Are Correct");
}
}
}
</script>
</body>
</html>