getElementById + parseInt problem

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
internalgrenade
Forum Newbie
Posts: 5
Joined: Mon Oct 04, 2010 7:29 pm

getElementById + parseInt problem

Post 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>
Last edited by Benjamin on Thu Oct 07, 2010 5:40 am, edited 1 time in total.
Reason: Added [syntax=html] tags.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: getElementById + parseInt problem

Post by VladSun »

Use the value property instead of innerHTML
There are 10 types of people in this world, those who understand binary and those who don't
internalgrenade
Forum Newbie
Posts: 5
Joined: Mon Oct 04, 2010 7:29 pm

Re: getElementById + parseInt problem

Post by internalgrenade »

i think that solved a problem but on line 12 i still have a infinite loop.
-Internal
internalgrenade
Forum Newbie
Posts: 5
Joined: Mon Oct 04, 2010 7:29 pm

Re: getElementById + parseInt problem

Post 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>
Last edited by Benjamin on Thu Oct 07, 2010 8:03 am, edited 1 time in total.
Reason: Added [syntax=html] tags.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: getElementById + parseInt problem

Post by VladSun »

VladSun wrote:Use the value property instead of innerHTML
Again ...
There are 10 types of people in this world, those who understand binary and those who don't
internalgrenade
Forum Newbie
Posts: 5
Joined: Mon Oct 04, 2010 7:29 pm

Re: getElementById + parseInt problem

Post 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>
Post Reply