11 replies [Last post]
rmcivo1
Offline
Enthusiast
Shropshire | Shrewsbury
Last seen: 9 years 45 weeks ago
Shropshire | Shrewsbury
Joined: 2003-12-05
Posts: 115
Points: 0

I am creating a quiz and i need this javascript that gives you an alert when you type in the write answer.

Has anybody got any clues
Rees

The Cyber World

Tags:
seb
Offline
Enthusiast
Last seen: 13 years 52 weeks ago
Joined: 2003-09-20
Posts: 208
Points: 0

Javascript to make quiz

function checkAnswer()
{
    if (document.forms[0].sAnswer.value=="poo poo")
    {
        alert("Well done, have a bone.");
    }
    else
    {
        alert("Bad boy!");
    }
}

rmcivo1
Offline
Enthusiast
Shropshire | Shrewsbury
Last seen: 9 years 45 weeks ago
Shropshire | Shrewsbury
Joined: 2003-12-05
Posts: 115
Points: 0

Thank You

Thank You, thats what i was just after

Rees
Laughing out loud

The Cyber World

seb
Offline
Enthusiast
Last seen: 13 years 52 weeks ago
Joined: 2003-09-20
Posts: 208
Points: 0

Javascript to make quiz

Of course you can make it more generic:

function checkAnswer(inputField,correctAnswer)
{
    if (document.getElementById("inputField").value==correctAnswer)
    {
        alert("Well done, have a bone.");
    }
    else
    {
        alert("Bad boy!");
    }
}

Note the case of everything in JavaScript is very important - and typing getElementById incorrectly is a common mistake.

rmcivo1
Offline
Enthusiast
Shropshire | Shrewsbury
Last seen: 9 years 45 weeks ago
Shropshire | Shrewsbury
Joined: 2003-12-05
Posts: 115
Points: 0

Javascript to make quiz

I understand the javascript, but i am not sure about id'ing the input field in the HTML

The Cyber World

seb
Offline
Enthusiast
Last seen: 13 years 52 weeks ago
Joined: 2003-09-20
Posts: 208
Points: 0

Javascript to make quiz

You have two choices. You can either give the input tag a name, as in

<form name="frm">
<input name="sAnswer" type="input" />
</form>

and then access it via the form name or array:

alert(document.forms[0].sAnswer.value);
alert(document.frm.sAnswer.value);

alternatively if you give the field an id:

<form name="frm">
<input id="sAnswer" name="sAnswer" type="input" />
</form>

then you can use getElementById()

alert(document.getElementById('sAnswer').value);

Note that in the second example the form tag becomes redundant, only needed if you are submitting the page somewhere.

I prefer to use the second option, always use a "getElementById" over a forms array access. You'll note I've given it both an id AND a name. Nothing wrong with that, and in fact it's essential if you're using ASP or similar since the request object built into ASP will only see input fields with a name attribute, not id.

Hope that's clearish.

rmcivo1
Offline
Enthusiast
Shropshire | Shrewsbury
Last seen: 9 years 45 weeks ago
Shropshire | Shrewsbury
Joined: 2003-12-05
Posts: 115
Points: 0

Javascript to make quiz

Thank you, I will try and get it working now

The Cyber World

rmcivo1
Offline
Enthusiast
Shropshire | Shrewsbury
Last seen: 9 years 45 weeks ago
Shropshire | Shrewsbury
Joined: 2003-12-05
Posts: 115
Points: 0

Javascript to make quiz

I cannot get the javascript to work, sorry i must say that i am a begginer and i don't know much about javascript

Thanks
Rees
Sad

The Cyber World

seb
Offline
Enthusiast
Last seen: 13 years 52 weeks ago
Joined: 2003-09-20
Posts: 208
Points: 0

Javascript to make quiz

How 'bout you post your source Smile

rmcivo1
Offline
Enthusiast
Shropshire | Shrewsbury
Last seen: 9 years 45 weeks ago
Shropshire | Shrewsbury
Joined: 2003-12-05
Posts: 115
Points: 0

Javascript to make quiz

<html>
<head>

<script language="javascript">

function Answer(Answer,correctanswer) 
{ 
    if (document.getElementById("Answer").value==answer) 
    { 
        alert("Well done, have a bone."); 
    } 
    else 
    { 
        alert("Bad boy!"); 
    } 
} 



</script>

</head>
<body>

<form name="Quiz">
<input type="text" size="30" maxlength="6" id="Answer" name="Answer">
</form>


</body>
</html>

The Cyber World

seb
Offline
Enthusiast
Last seen: 13 years 52 weeks ago
Joined: 2003-09-20
Posts: 208
Points: 0

Javascript to make quiz

Your problem is at no point have you connected the input field with the javascript function. You need some sort of event.

My take:

<html>
<head>

<script language="javascript">

function Answer(Answer,correctanswer) 
{ 
    if (document.getElementById(Answer).value==correctanswer) 
    { 
        alert("Well done, have a bone."); 
    } 
    else 
    { 
        alert("Bad boy!"); 
    } 
} 



</script>

</head>
<body>

<form name="Quiz">
<input type="text" size="30" maxlength="6" id="Answer" name="Answer">

<input type="button" value="check answer" onclick="Answer('Answer','42');">
</form>


</body>
</html>

Also I would advise against calling everything "Answer", makes the code confusing. Instead maybe call the function "checkAnswer", the input field "inptAnswer" etc etc.

rmcivo1
Offline
Enthusiast
Shropshire | Shrewsbury
Last seen: 9 years 45 weeks ago
Shropshire | Shrewsbury
Joined: 2003-12-05
Posts: 115
Points: 0

Javascript to make quiz

I know i am a pain would u mind setting up one question and answer quiz then i can work on it from there.

Thanks
Rees

The Cyber World