Hello there, this is my first post here, and I've just started javascript. My problem is that I get an undefined error on this page after i write the result and click on the "Click Here to vote" button.
<script type="text/javascript">
var a = Math.ceil(Math.random() * 20);
var b = Math.ceil(Math.random() * 20);
var c = a + b
function DrawBotBoot()
{
document.write("What is "+ a + " + " + b +"? ");
document.write("<input id='BotBootInput' type='text' maxlength='2' size='2'/>");
}
function ValidBotBoot(){
var d = document.getElementById('BotBootInput').value;
if (d == c){
window.location = "{$list_url}/index.php?a=in&u={$username}&sid={$sid}";
}
else
return false;
}
</script>
</head>
<body>
Are you human?<br />
<script type="text/javascript">DrawBotBoot()</script>
<input id="Button1" type="button" value="Click Here to Vote" onclick="alert(ValidBotBoot());"/>
The original code :
Code:
<script type="text/javascript">
var a = Math.ceil(Math.random() * 10);
var b = Math.ceil(Math.random() * 10);
var c = a + b
function DrawBotBoot()
{
document.write("What is "+ a + " + " + b +"? ");
document.write("<input id='BotBootInput' type='text' maxlength='2' size='2'/>");
}
function ValidBotBoot(){
var d = document.getElementById('BotBootInput').value;
if (d == c) return true;
return false;
}
</script>
</head>
<body>
Are you human?<br />
<script type="text/javascript">DrawBotBoot()</script>
<input id="Button1" type="button" value="Check" onclick="alert(ValidBotBoot());"/>
You don't have an undefined error, you're simply opening a message box with an undefined argument:
Code:
onclick="alert(ValidBotBoot());"
If the user gives the right answer, ValidBotBoot() doesn't return anything, therefore the output is undefined. Return true in that case and/or add an if clause
<script type="text/javascript">
var a = Math.ceil(Math.random() * 20);
var b = Math.ceil(Math.random() * 20);
var c = a + b
function DrawBotBoot()
{
document.write("What is "+ a + " + " + b +"? ");
document.write("<input id='BotBootInput' type='text' maxlength='2' size='2'/>");
}
function ValidBotBoot(){
var d = document.getElementById('BotBootInput').value;
if (d == c) return true;
(alert("Wrong Code"));
document.getElementById('tryagain').value();
return 'Wrong Answer'; }
</script>
I am trying to Target a span ID, <span id="tryagain"></span>
Bookmarks